1 | /*
|
---|
2 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
3 | *
|
---|
4 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
5 | * available from http://www.virtualbox.org. This file is free software;
|
---|
6 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
7 | * General Public License (GPL) as published by the Free Software
|
---|
8 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
9 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
10 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
11 | * --------------------------------------------------------------------
|
---|
12 | *
|
---|
13 | * This code is based on:
|
---|
14 | *
|
---|
15 | * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
|
---|
16 | *
|
---|
17 | * Copyright (C) 2002 MandrakeSoft S.A.
|
---|
18 | *
|
---|
19 | * MandrakeSoft S.A.
|
---|
20 | * 43, rue d'Aboukir
|
---|
21 | * 75002 Paris - France
|
---|
22 | * http://www.linux-mandrake.com/
|
---|
23 | * http://www.mandrakesoft.com/
|
---|
24 | *
|
---|
25 | * This library is free software; you can redistribute it and/or
|
---|
26 | * modify it under the terms of the GNU Lesser General Public
|
---|
27 | * License as published by the Free Software Foundation; either
|
---|
28 | * version 2 of the License, or (at your option) any later version.
|
---|
29 | *
|
---|
30 | * This library is distributed in the hope that it will be useful,
|
---|
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
33 | * Lesser General Public License for more details.
|
---|
34 | *
|
---|
35 | * You should have received a copy of the GNU Lesser General Public
|
---|
36 | * License along with this library; if not, write to the Free Software
|
---|
37 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
38 | *
|
---|
39 | */
|
---|
40 |
|
---|
41 |
|
---|
42 | #include <stdint.h>
|
---|
43 | #include "inlines.h"
|
---|
44 | #include "biosint.h"
|
---|
45 |
|
---|
46 | extern uint16_t get_floppy_dpt(uint8_t drive_type);
|
---|
47 |
|
---|
48 | //////////////////////
|
---|
49 | // FLOPPY functions //
|
---|
50 | //////////////////////
|
---|
51 |
|
---|
52 | void set_diskette_ret_status(uint8_t value)
|
---|
53 | {
|
---|
54 | write_byte(0x0040, 0x0041, value);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void set_diskette_current_cyl(uint8_t drive, uint8_t cyl)
|
---|
58 | {
|
---|
59 | if (drive > 1)
|
---|
60 | BX_PANIC("set_diskette_current_cyl: drive > 1\n");
|
---|
61 | write_byte(0x0040, 0x0094+drive, cyl);
|
---|
62 | }
|
---|
63 |
|
---|
64 | #if 1 //BX_SUPPORT_FLOPPY
|
---|
65 |
|
---|
66 | #if DEBUG_INT13_FL
|
---|
67 | # define BX_DEBUG_INT13_FL(...) BX_DEBUG(__VA_ARGS__)
|
---|
68 | #else
|
---|
69 | # define BX_DEBUG_INT13_FL(...)
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
|
---|
73 |
|
---|
74 | extern int diskette_param_table; /* At a fixed location. */
|
---|
75 |
|
---|
76 | #ifndef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Wait for the 7th bit of 0040:003e to be set by int0e_handler.
|
---|
80 | * @returns first 7 bits of byte 0040:003e, interrupts disabled.
|
---|
81 | */
|
---|
82 | uint8_t floppy_wait_for_interrupt(void)
|
---|
83 | {
|
---|
84 | int_disable();
|
---|
85 | for (;;)
|
---|
86 | {
|
---|
87 | uint8_t val8 = read_byte(0x0040, 0x003e);
|
---|
88 | if (val8 & 0x80)
|
---|
89 | return val8 & ~0x7f;
|
---|
90 | int_enable_hlt_disable();
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Wait for the 7th bit of 0040:003e to be set by int0e_handler or 0040:0040 to
|
---|
96 | * be cleared by the timer, clearing the interrupt flag on success.
|
---|
97 | *
|
---|
98 | * @returns 0 on timeout with interrupts enabled.
|
---|
99 | * All 8 bits at 0040:003e on interrupt with interrupts disabled (i.e.
|
---|
100 | * non-zero), after first clearing the 7th bit at 0040:003e.
|
---|
101 | */
|
---|
102 | uint8_t floppy_wait_for_interrupt_or_timeout(void)
|
---|
103 | {
|
---|
104 | int_disable();
|
---|
105 | for (;;)
|
---|
106 | {
|
---|
107 | uint8_t val8 = read_byte(0x0040, 0x0040);
|
---|
108 | if (val8 == 0) {
|
---|
109 | int_enable();
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | val8 = read_byte(0x0040, 0x003e);
|
---|
114 | if (val8 & 0x80) {
|
---|
115 | write_byte(0x0040, 0x003e, val8 & 0x7f);
|
---|
116 | return val8;
|
---|
117 | }
|
---|
118 | int_enable_hlt_disable();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | #endif /* !VBOX_WITH_FLOPPY_IRQ_POLLING */
|
---|
123 |
|
---|
124 | void floppy_reset_controller(uint16_t drive)
|
---|
125 | {
|
---|
126 | uint8_t val8;
|
---|
127 |
|
---|
128 | // Reset controller
|
---|
129 | val8 = inb(0x03f2);
|
---|
130 | outb(0x03f2, val8 & ~0x04);
|
---|
131 | outb(0x03f2, val8 | 0x04);
|
---|
132 |
|
---|
133 | // Wait for controller to come out of reset
|
---|
134 | do {
|
---|
135 | val8 = inb(0x3f4);
|
---|
136 | } while ( (val8 & 0xc0) != 0x80 );
|
---|
137 |
|
---|
138 | // Mark media in drive as unknown
|
---|
139 | val8 = read_byte(0x0040, 0x0090 + drive);
|
---|
140 | val8 &= ~0x10;
|
---|
141 | write_byte(0x0040, 0x90 + drive, val8);
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | void floppy_prepare_controller(uint16_t drive)
|
---|
146 | {
|
---|
147 | uint8_t val8, dor, prev_reset;
|
---|
148 |
|
---|
149 | // set 40:3e bit 7 to 0
|
---|
150 | val8 = read_byte(0x0040, 0x003e);
|
---|
151 | val8 &= 0x7f;
|
---|
152 | write_byte(0x0040, 0x003e, val8);
|
---|
153 |
|
---|
154 | // turn on motor of selected drive, DMA & int enabled, normal operation
|
---|
155 | prev_reset = inb(0x03f2) & 0x04;
|
---|
156 | if (drive)
|
---|
157 | dor = 0x20;
|
---|
158 | else
|
---|
159 | dor = 0x10;
|
---|
160 | dor |= 0x0c;
|
---|
161 | dor |= drive;
|
---|
162 | outb(0x03f2, dor);
|
---|
163 |
|
---|
164 | // reset the disk motor timeout value of INT 08
|
---|
165 | write_byte(0x0040,0x0040, BX_FLOPPY_ON_CNT);
|
---|
166 |
|
---|
167 | // program data rate
|
---|
168 | val8 = read_byte(0x0040, 0x008b);
|
---|
169 | val8 >>= 6;
|
---|
170 | outb(0x03f7, val8);
|
---|
171 |
|
---|
172 | // wait for drive readiness
|
---|
173 | do {
|
---|
174 | val8 = inb(0x3f4);
|
---|
175 | } while ( (val8 & 0xc0) != 0x80 );
|
---|
176 |
|
---|
177 | if (prev_reset == 0) {
|
---|
178 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
179 | // turn on interrupts
|
---|
180 | int_enable();
|
---|
181 | // wait on 40:3e bit 7 to become 1
|
---|
182 | do {
|
---|
183 | val8 = read_byte(0x0040, 0x003e);
|
---|
184 | } while ( (val8 & 0x80) == 0 );
|
---|
185 | val8 &= 0x7f;
|
---|
186 | int_disable();
|
---|
187 | #else
|
---|
188 | val8 = floppy_wait_for_interrupt(); /* (7th bit cleared in ret val) */
|
---|
189 | #endif
|
---|
190 | write_byte(0x0040, 0x003e, val8);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | bx_bool floppy_media_known(uint16_t drive)
|
---|
195 | {
|
---|
196 | uint8_t val8;
|
---|
197 | uint16_t media_state_offset;
|
---|
198 |
|
---|
199 | val8 = read_byte(0x0040, 0x003e); // diskette recal status
|
---|
200 | if (drive)
|
---|
201 | val8 >>= 1;
|
---|
202 | val8 &= 0x01;
|
---|
203 | if (val8 == 0)
|
---|
204 | return 0;
|
---|
205 |
|
---|
206 | media_state_offset = 0x0090;
|
---|
207 | if (drive)
|
---|
208 | media_state_offset += 1;
|
---|
209 |
|
---|
210 | val8 = read_byte(0x0040, media_state_offset);
|
---|
211 | val8 = (val8 >> 4) & 0x01;
|
---|
212 | if (val8 == 0)
|
---|
213 | return 0;
|
---|
214 |
|
---|
215 | // checks passed, return KNOWN
|
---|
216 | return 1;
|
---|
217 | }
|
---|
218 |
|
---|
219 | bx_bool floppy_read_id(uint16_t drive)
|
---|
220 | {
|
---|
221 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
222 | uint8_t val8;
|
---|
223 | #endif
|
---|
224 | uint8_t return_status[7];
|
---|
225 | int i;
|
---|
226 |
|
---|
227 | floppy_prepare_controller(drive);
|
---|
228 |
|
---|
229 | // send Read ID command (2 bytes) to controller
|
---|
230 | outb(0x03f5, 0x4a); // 4a: Read ID (MFM)
|
---|
231 | outb(0x03f5, drive); // 0=drive0, 1=drive1, head always 0
|
---|
232 |
|
---|
233 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
234 | // turn on interrupts
|
---|
235 | int_enable();
|
---|
236 |
|
---|
237 | // wait on 40:3e bit 7 to become 1
|
---|
238 | do {
|
---|
239 | val8 = (read_byte(0x0040, 0x003e) & 0x80);
|
---|
240 | } while ( val8 == 0 );
|
---|
241 |
|
---|
242 | val8 = 0; // separate asm from while() loop
|
---|
243 | // turn off interrupts
|
---|
244 | int_disable();
|
---|
245 | #else
|
---|
246 | floppy_wait_for_interrupt();
|
---|
247 | #endif
|
---|
248 |
|
---|
249 | // read 7 return status bytes from controller
|
---|
250 | for (i = 0; i < 7; ++i) {
|
---|
251 | return_status[i] = inb(0x3f5);
|
---|
252 | }
|
---|
253 |
|
---|
254 | if ( (return_status[0] & 0xc0) != 0 )
|
---|
255 | return 0;
|
---|
256 | else
|
---|
257 | return 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | bx_bool floppy_drive_recal(uint16_t drive)
|
---|
261 | {
|
---|
262 | uint8_t val8;
|
---|
263 | uint16_t curr_cyl_offset;
|
---|
264 |
|
---|
265 | floppy_prepare_controller(drive);
|
---|
266 |
|
---|
267 | // send Recalibrate command (2 bytes) to controller
|
---|
268 | outb(0x03f5, 0x07); // 07: Recalibrate
|
---|
269 | outb(0x03f5, drive); // 0=drive0, 1=drive1
|
---|
270 |
|
---|
271 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
272 | // turn on interrupts
|
---|
273 | int_enable();
|
---|
274 |
|
---|
275 | // wait on 40:3e bit 7 to become 1
|
---|
276 | do {
|
---|
277 | val8 = (read_byte(0x0040, 0x003e) & 0x80);
|
---|
278 | } while ( val8 == 0 );
|
---|
279 |
|
---|
280 | val8 = 0; // separate asm from while() loop
|
---|
281 | // turn off interrupts
|
---|
282 | int_disable();
|
---|
283 |
|
---|
284 | // set 40:3e bit 7 to 0, and calibrated bit
|
---|
285 | val8 = read_byte(0x0040, 0x003e);
|
---|
286 | val8 &= 0x7f;
|
---|
287 | #else
|
---|
288 | val8 = floppy_wait_for_interrupt(); /* (7th bit cleared in ret val) */
|
---|
289 |
|
---|
290 | // set 40:3e bit 7 to 0, and calibrated bit
|
---|
291 | #endif
|
---|
292 | if (drive) {
|
---|
293 | val8 |= 0x02; // Drive 1 calibrated
|
---|
294 | curr_cyl_offset = 0x0095;
|
---|
295 | } else {
|
---|
296 | val8 |= 0x01; // Drive 0 calibrated
|
---|
297 | curr_cyl_offset = 0x0094;
|
---|
298 | }
|
---|
299 | write_byte(0x0040, 0x003e, val8);
|
---|
300 | write_byte(0x0040, curr_cyl_offset, 0); // current cylinder is 0
|
---|
301 |
|
---|
302 | return 1;
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | bx_bool floppy_media_sense(uint16_t drive)
|
---|
307 | {
|
---|
308 | bx_bool retval;
|
---|
309 | uint16_t media_state_offset;
|
---|
310 | uint8_t drive_type, config_data, media_state;
|
---|
311 |
|
---|
312 | if (floppy_drive_recal(drive) == 0)
|
---|
313 | return 0;
|
---|
314 |
|
---|
315 | // Try the diskette data rates in the following order:
|
---|
316 | // 1 Mbps -> 500 Kbps -> 300 Kbps -> 250 Kbps
|
---|
317 | // The 1 Mbps rate is only tried for 2.88M drives.
|
---|
318 |
|
---|
319 | // ** config_data **
|
---|
320 | // Bitfields for diskette media control:
|
---|
321 | // Bit(s) Description (Table M0028)
|
---|
322 | // 7-6 last data rate set by controller
|
---|
323 | // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
|
---|
324 | // 5-4 last diskette drive step rate selected
|
---|
325 | // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
|
---|
326 | // 3-2 {data rate at start of operation}
|
---|
327 | // 1-0 reserved
|
---|
328 |
|
---|
329 | // ** media_state **
|
---|
330 | // Bitfields for diskette drive media state:
|
---|
331 | // Bit(s) Description (Table M0030)
|
---|
332 | // 7-6 data rate
|
---|
333 | // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
|
---|
334 | // 5 double stepping required (e.g. 360kB in 1.2MB)
|
---|
335 | // 4 media type established
|
---|
336 | // 3 drive capable of supporting 4MB media
|
---|
337 | // 2-0 on exit from BIOS, contains
|
---|
338 | // 000 trying 360kB in 360kB
|
---|
339 | // 001 trying 360kB in 1.2MB
|
---|
340 | // 010 trying 1.2MB in 1.2MB
|
---|
341 | // 011 360kB in 360kB established
|
---|
342 | // 100 360kB in 1.2MB established
|
---|
343 | // 101 1.2MB in 1.2MB established
|
---|
344 | // 110 reserved
|
---|
345 | // 111 all other formats/drives
|
---|
346 |
|
---|
347 | // @todo: break out drive type determination
|
---|
348 | drive_type = inb_cmos(0x10);
|
---|
349 | if (drive == 0)
|
---|
350 | drive_type >>= 4;
|
---|
351 | else
|
---|
352 | drive_type &= 0x0f;
|
---|
353 | if ( drive_type == 1 ) {
|
---|
354 | // 360K 5.25" drive
|
---|
355 | config_data = 0x00; // 0000 0000
|
---|
356 | media_state = 0x15; // 0001 0101
|
---|
357 | retval = 1;
|
---|
358 | }
|
---|
359 | else if ( drive_type == 2 ) {
|
---|
360 | // 1.2 MB 5.25" drive
|
---|
361 | config_data = 0x00; // 0000 0000
|
---|
362 | media_state = 0x35; // 0011 0101 // need double stepping??? (bit 5)
|
---|
363 | retval = 1;
|
---|
364 | }
|
---|
365 | else if ( drive_type == 3 ) {
|
---|
366 | // 720K 3.5" drive
|
---|
367 | config_data = 0x00; // 0000 0000 ???
|
---|
368 | media_state = 0x17; // 0001 0111
|
---|
369 | retval = 1;
|
---|
370 | }
|
---|
371 | else if ( drive_type == 4 ) {
|
---|
372 | // 1.44 MB 3.5" drive
|
---|
373 | config_data = 0x00; // 0000 0000
|
---|
374 | media_state = 0x17; // 0001 0111
|
---|
375 | retval = 1;
|
---|
376 | }
|
---|
377 | else if ( drive_type == 5 ) {
|
---|
378 | // 2.88 MB 3.5" drive
|
---|
379 | config_data = 0xCC; // 1100 1100
|
---|
380 | media_state = 0xD7; // 1101 0111
|
---|
381 | retval = 1;
|
---|
382 | }
|
---|
383 | // Extended floppy size uses special cmos setting
|
---|
384 | else if ( drive_type == 14 || drive_type == 15 ) {
|
---|
385 | // 15.6 MB 3.5" (fake) || 63.5 MB 3.5" (fake) - report same as 2.88 MB.
|
---|
386 | config_data = 0xCC; // 1100 1100
|
---|
387 | media_state = 0xD7; // 1101 0111
|
---|
388 | retval = 1;
|
---|
389 | }
|
---|
390 | else {
|
---|
391 | // not recognized
|
---|
392 | config_data = 0x00; // 0000 0000
|
---|
393 | media_state = 0x00; // 0000 0000
|
---|
394 | retval = 0;
|
---|
395 | }
|
---|
396 |
|
---|
397 | write_byte(0x0040, 0x008B, config_data);
|
---|
398 | while (!floppy_read_id(drive)) {
|
---|
399 | if ((config_data & 0xC0) == 0x80) {
|
---|
400 | // If even 250 Kbps failed, we can't do much
|
---|
401 | break;
|
---|
402 | }
|
---|
403 | switch (config_data & 0xC0) {
|
---|
404 | case 0xC0: // 1 Mbps
|
---|
405 | config_data = config_data & 0x3F | 0x00;
|
---|
406 | break;
|
---|
407 | case 0x00: // 500 Kbps
|
---|
408 | config_data = config_data & 0x3F | 0x40;
|
---|
409 | break;
|
---|
410 | case 0x40: // 300 Kbps
|
---|
411 | config_data = config_data & 0x3F | 0x80;
|
---|
412 | break;
|
---|
413 | }
|
---|
414 | write_byte(0x0040, 0x008B, config_data);
|
---|
415 | }
|
---|
416 |
|
---|
417 | if (drive == 0)
|
---|
418 | media_state_offset = 0x0090;
|
---|
419 | else
|
---|
420 | media_state_offset = 0x0091;
|
---|
421 | write_byte(0x0040, 0x008B, config_data);
|
---|
422 | write_byte(0x0040, media_state_offset, media_state);
|
---|
423 |
|
---|
424 | return retval;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | bx_bool floppy_drive_exists(uint16_t drive)
|
---|
429 | {
|
---|
430 | uint8_t drive_type;
|
---|
431 |
|
---|
432 | // check CMOS to see if drive exists
|
---|
433 | // @todo: break out drive type determination
|
---|
434 | drive_type = inb_cmos(0x10);
|
---|
435 | if (drive == 0)
|
---|
436 | drive_type >>= 4;
|
---|
437 | else
|
---|
438 | drive_type &= 0x0f;
|
---|
439 | return drive_type != 0;
|
---|
440 | }
|
---|
441 |
|
---|
442 | //@todo: put in a header
|
---|
443 | #define AX r.gr.u.r16.ax
|
---|
444 | #define BX r.gr.u.r16.bx
|
---|
445 | #define CX r.gr.u.r16.cx
|
---|
446 | #define DX r.gr.u.r16.dx
|
---|
447 | #define SI r.gr.u.r16.si
|
---|
448 | #define DI r.gr.u.r16.di
|
---|
449 | #define BP r.gr.u.r16.bp
|
---|
450 | #define ELDX r.gr.u.r16.sp
|
---|
451 | #define DS r.ds
|
---|
452 | #define ES r.es
|
---|
453 | #define FLAGS r.ra.flags.u.r16.flags
|
---|
454 |
|
---|
455 | void BIOSCALL int13_diskette_function(disk_regs_t r)
|
---|
456 | {
|
---|
457 | uint8_t drive, num_sectors, track, sector, head;
|
---|
458 | uint16_t base_address, base_count, base_es;
|
---|
459 | uint8_t page, mode_register, val8, media_state;
|
---|
460 | uint8_t return_status[7];
|
---|
461 | uint8_t drive_type, num_floppies, ah;
|
---|
462 | uint16_t last_addr;
|
---|
463 | int i;
|
---|
464 |
|
---|
465 | BX_DEBUG_INT13_FL("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
|
---|
466 |
|
---|
467 | ah = GET_AH();
|
---|
468 |
|
---|
469 | switch ( ah ) {
|
---|
470 | case 0x00: // diskette controller reset
|
---|
471 | BX_DEBUG_INT13_FL("floppy f00\n");
|
---|
472 | drive = GET_ELDL();
|
---|
473 | if (drive > 1) {
|
---|
474 | SET_AH(1); // invalid param
|
---|
475 | set_diskette_ret_status(1);
|
---|
476 | SET_CF();
|
---|
477 | return;
|
---|
478 | }
|
---|
479 | // @todo: break out drive type determination
|
---|
480 | drive_type = inb_cmos(0x10);
|
---|
481 | if (drive == 0)
|
---|
482 | drive_type >>= 4;
|
---|
483 | else
|
---|
484 | drive_type &= 0x0f;
|
---|
485 | if (drive_type == 0) {
|
---|
486 | SET_AH(0x80); // drive not responding
|
---|
487 | set_diskette_ret_status(0x80);
|
---|
488 | SET_CF();
|
---|
489 | return;
|
---|
490 | }
|
---|
491 |
|
---|
492 | // force re-calibration etc.
|
---|
493 | write_byte(0x0040, 0x003e, 0);
|
---|
494 |
|
---|
495 | SET_AH(0);
|
---|
496 | set_diskette_ret_status(0);
|
---|
497 | CLEAR_CF(); // successful
|
---|
498 | set_diskette_current_cyl(drive, 0); // current cylinder
|
---|
499 | return;
|
---|
500 |
|
---|
501 | case 0x01: // Read Diskette Status
|
---|
502 | CLEAR_CF();
|
---|
503 | val8 = read_byte(0x0000, 0x0441);
|
---|
504 | SET_AH(val8);
|
---|
505 | if (val8) {
|
---|
506 | SET_CF();
|
---|
507 | }
|
---|
508 | return;
|
---|
509 |
|
---|
510 | case 0x02: // Read Diskette Sectors
|
---|
511 | case 0x03: // Write Diskette Sectors
|
---|
512 | case 0x04: // Verify Diskette Sectors
|
---|
513 | num_sectors = GET_AL();
|
---|
514 | track = GET_CH();
|
---|
515 | sector = GET_CL();
|
---|
516 | head = GET_DH();
|
---|
517 | drive = GET_ELDL();
|
---|
518 |
|
---|
519 | if ( (drive > 1) || (head > 1) ||
|
---|
520 | (num_sectors == 0) || (num_sectors > 72) ) {
|
---|
521 | BX_INFO("%s: drive>1 || head>1 ...\n", __func__);
|
---|
522 | SET_AH(1);
|
---|
523 | set_diskette_ret_status(1);
|
---|
524 | SET_AL(0); // no sectors read
|
---|
525 | SET_CF(); // error occurred
|
---|
526 | return;
|
---|
527 | }
|
---|
528 |
|
---|
529 | // see if drive exists
|
---|
530 | if (floppy_drive_exists(drive) == 0) {
|
---|
531 | BX_DEBUG_INT13_FL("failed (not ready)\n");
|
---|
532 | SET_AH(0x80); // not responding
|
---|
533 | set_diskette_ret_status(0x80);
|
---|
534 | SET_AL(0); // no sectors read
|
---|
535 | SET_CF(); // error occurred
|
---|
536 | return;
|
---|
537 | }
|
---|
538 |
|
---|
539 | // see if media in drive, and type is known
|
---|
540 | if (floppy_media_known(drive) == 0) {
|
---|
541 | if (floppy_media_sense(drive) == 0) {
|
---|
542 | BX_DEBUG_INT13_FL("media not found\n");
|
---|
543 | SET_AH(0x0C); // Media type not found
|
---|
544 | set_diskette_ret_status(0x0C);
|
---|
545 | SET_AL(0); // no sectors read
|
---|
546 | SET_CF(); // error occurred
|
---|
547 | return;
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | if (ah == 0x02) {
|
---|
552 | // Read Diskette Sectors
|
---|
553 |
|
---|
554 | //-----------------------------------
|
---|
555 | // set up DMA controller for transfer
|
---|
556 | //-----------------------------------
|
---|
557 |
|
---|
558 | // es:bx = pointer to where to place information from diskette
|
---|
559 | // port 04: DMA-1 base and current address, channel 2
|
---|
560 | // port 05: DMA-1 base and current count, channel 2
|
---|
561 | // @todo: merge/factor out pointer normalization
|
---|
562 | page = (ES >> 12); // upper 4 bits
|
---|
563 | base_es = (ES << 4); // lower 16bits contributed by ES
|
---|
564 | base_address = base_es + BX; // lower 16 bits of address
|
---|
565 | // contributed by ES:BX
|
---|
566 | if ( base_address < base_es ) {
|
---|
567 | // in case of carry, adjust page by 1
|
---|
568 | page++;
|
---|
569 | }
|
---|
570 | base_count = (num_sectors * 512) - 1;
|
---|
571 |
|
---|
572 | // check for 64K boundary overrun
|
---|
573 | last_addr = base_address + base_count;
|
---|
574 | if (last_addr < base_address) {
|
---|
575 | SET_AH(0x09);
|
---|
576 | set_diskette_ret_status(0x09);
|
---|
577 | SET_AL(0); // no sectors read
|
---|
578 | SET_CF(); // error occurred
|
---|
579 | return;
|
---|
580 | }
|
---|
581 |
|
---|
582 | BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
|
---|
583 | outb(0x000a, 0x06);
|
---|
584 |
|
---|
585 | BX_DEBUG_INT13_FL("clear flip-flop\n");
|
---|
586 | outb(0x000c, 0x00); // clear flip-flop
|
---|
587 | outb(0x0004, base_address);
|
---|
588 | outb(0x0004, base_address>>8);
|
---|
589 | BX_DEBUG_INT13_FL("clear flip-flop\n");
|
---|
590 | outb(0x000c, 0x00); // clear flip-flop
|
---|
591 | outb(0x0005, base_count);
|
---|
592 | outb(0x0005, base_count>>8);
|
---|
593 | BX_DEBUG_INT13_FL("xfer buf %x bytes at %x:%x\n",
|
---|
594 | base_count + 1, page, base_address);
|
---|
595 |
|
---|
596 | // port 0b: DMA-1 Mode Register
|
---|
597 | mode_register = 0x46; // single mode, increment, autoinit disable,
|
---|
598 | // transfer type=write, channel 2
|
---|
599 | BX_DEBUG_INT13_FL("setting mode register\n");
|
---|
600 | outb(0x000b, mode_register);
|
---|
601 |
|
---|
602 | BX_DEBUG_INT13_FL("setting page register\n");
|
---|
603 | // port 81: DMA-1 Page Register, channel 2
|
---|
604 | outb(0x0081, page);
|
---|
605 |
|
---|
606 | BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
|
---|
607 | outb(0x000a, 0x02); // unmask channel 2
|
---|
608 |
|
---|
609 | //--------------------------------------
|
---|
610 | // set up floppy controller for transfer
|
---|
611 | //--------------------------------------
|
---|
612 | floppy_prepare_controller(drive);
|
---|
613 |
|
---|
614 | // send read-normal-data command (9 bytes) to controller
|
---|
615 | outb(0x03f5, 0xe6); // e6: read normal data
|
---|
616 | outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
|
---|
617 | outb(0x03f5, track);
|
---|
618 | outb(0x03f5, head);
|
---|
619 | outb(0x03f5, sector);
|
---|
620 | outb(0x03f5, 2); // 512 byte sector size
|
---|
621 | outb(0x03f5, sector + num_sectors - 1); // last sector to read on track
|
---|
622 | outb(0x03f5, 0); // Gap length
|
---|
623 | outb(0x03f5, 0xff); // Gap length
|
---|
624 | BX_DEBUG_INT13_FL("read initiated\n");
|
---|
625 |
|
---|
626 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
627 | // turn on interrupts
|
---|
628 | int_enable();
|
---|
629 |
|
---|
630 | // wait on 40:3e bit 7 to become 1 or timeout (latter isn't armed so it won't happen)
|
---|
631 | do {
|
---|
632 | val8 = read_byte(0x0040, 0x0040);
|
---|
633 | if (val8 == 0) {
|
---|
634 | BX_DEBUG_INT13_FL("failed (not ready)\n");
|
---|
635 | floppy_reset_controller(drive);
|
---|
636 | SET_AH(0x80); // drive not ready (timeout)
|
---|
637 | set_diskette_ret_status(0x80);
|
---|
638 | SET_AL(0); // no sectors read
|
---|
639 | SET_CF(); // error occurred
|
---|
640 | return;
|
---|
641 | }
|
---|
642 | val8 = (read_byte(0x0040, 0x003e) & 0x80);
|
---|
643 | } while ( val8 == 0 );
|
---|
644 |
|
---|
645 | val8 = 0; // separate asm from while() loop
|
---|
646 | // turn off interrupts
|
---|
647 | int_disable();
|
---|
648 |
|
---|
649 | // set 40:3e bit 7 to 0
|
---|
650 | val8 = read_byte(0x0040, 0x003e);
|
---|
651 | val8 &= 0x7f;
|
---|
652 | write_byte(0x0040, 0x003e, val8);
|
---|
653 |
|
---|
654 | #else
|
---|
655 | val8 = floppy_wait_for_interrupt_or_timeout();
|
---|
656 | if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
|
---|
657 | BX_DEBUG_INT13_FL("failed (not ready)\n");
|
---|
658 | floppy_reset_controller(drive);
|
---|
659 | SET_AH(0x80); // drive not ready (timeout)
|
---|
660 | set_diskette_ret_status(0x80);
|
---|
661 | SET_AL(0); // no sectors read
|
---|
662 | SET_CF(); // error occurred
|
---|
663 | return;
|
---|
664 | }
|
---|
665 | #endif
|
---|
666 |
|
---|
667 | // check port 3f4 for accessibility to status bytes
|
---|
668 | val8 = inb(0x3f4);
|
---|
669 | if ( (val8 & 0xc0) != 0xc0 )
|
---|
670 | BX_PANIC("%s: ctrl not ready\n", __func__);
|
---|
671 |
|
---|
672 | // read 7 return status bytes from controller and store in BDA
|
---|
673 | for (i = 0; i < 7; ++i) {
|
---|
674 | return_status[i] = inb(0x3f5);
|
---|
675 | write_byte(0x0040, 0x0042 + i, return_status[i]);
|
---|
676 | }
|
---|
677 |
|
---|
678 | if ( (return_status[0] & 0xc0) != 0 ) {
|
---|
679 | BX_DEBUG_INT13_FL("failed (FDC failure)\n");
|
---|
680 | floppy_reset_controller(drive);
|
---|
681 | SET_AH(0x20);
|
---|
682 | set_diskette_ret_status(0x20);
|
---|
683 | SET_AL(0); // no sectors read
|
---|
684 | SET_CF(); // error occurred
|
---|
685 | return;
|
---|
686 | }
|
---|
687 |
|
---|
688 | #ifdef DMA_WORKAROUND
|
---|
689 | rep_movsw(ES :> BX, ES :> BX, num_sectors * 512 / 2);
|
---|
690 | #endif
|
---|
691 | BX_DEBUG_INT13_FL("success!\n");
|
---|
692 | // ??? should track be new val from return_status[3] ?
|
---|
693 | set_diskette_current_cyl(drive, track);
|
---|
694 | // AL = number of sectors read (same value as passed)
|
---|
695 | SET_AH(0x00); // success
|
---|
696 | CLEAR_CF(); // success
|
---|
697 | return;
|
---|
698 | } else if (ah == 0x03) {
|
---|
699 | // Write Diskette Sectors
|
---|
700 |
|
---|
701 | //-----------------------------------
|
---|
702 | // set up DMA controller for transfer
|
---|
703 | //-----------------------------------
|
---|
704 |
|
---|
705 | // es:bx = pointer to where to place information from diskette
|
---|
706 | // port 04: DMA-1 base and current address, channel 2
|
---|
707 | // port 05: DMA-1 base and current count, channel 2
|
---|
708 | // @todo: merge/factor out pointer normalization
|
---|
709 | page = (ES >> 12); // upper 4 bits
|
---|
710 | base_es = (ES << 4); // lower 16bits contributed by ES
|
---|
711 | base_address = base_es + BX; // lower 16 bits of address
|
---|
712 | // contributed by ES:BX
|
---|
713 | if ( base_address < base_es ) {
|
---|
714 | // in case of carry, adjust page by 1
|
---|
715 | page++;
|
---|
716 | }
|
---|
717 | base_count = (num_sectors * 512) - 1;
|
---|
718 |
|
---|
719 | // check for 64K boundary overrun
|
---|
720 | last_addr = base_address + base_count;
|
---|
721 | if (last_addr < base_address) {
|
---|
722 | SET_AH(0x09);
|
---|
723 | set_diskette_ret_status(0x09);
|
---|
724 | SET_AL(0); // no sectors read
|
---|
725 | SET_CF(); // error occurred
|
---|
726 | return;
|
---|
727 | }
|
---|
728 |
|
---|
729 | BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
|
---|
730 | outb(0x000a, 0x06);
|
---|
731 |
|
---|
732 | outb(0x000c, 0x00); // clear flip-flop
|
---|
733 | outb(0x0004, base_address);
|
---|
734 | outb(0x0004, base_address>>8);
|
---|
735 | outb(0x000c, 0x00); // clear flip-flop
|
---|
736 | outb(0x0005, base_count);
|
---|
737 | outb(0x0005, base_count>>8);
|
---|
738 | BX_DEBUG_INT13_FL("xfer buf %x bytes at %x:%x\n",
|
---|
739 | base_count, page, base_address);
|
---|
740 |
|
---|
741 | // port 0b: DMA-1 Mode Register
|
---|
742 | mode_register = 0x4a; // single mode, increment, autoinit disable,
|
---|
743 | // transfer type=read, channel 2
|
---|
744 | outb(0x000b, mode_register);
|
---|
745 |
|
---|
746 | // port 81: DMA-1 Page Register, channel 2
|
---|
747 | outb(0x0081, page);
|
---|
748 |
|
---|
749 | BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
|
---|
750 | outb(0x000a, 0x02);
|
---|
751 |
|
---|
752 | //--------------------------------------
|
---|
753 | // set up floppy controller for transfer
|
---|
754 | //--------------------------------------
|
---|
755 | floppy_prepare_controller(drive);
|
---|
756 |
|
---|
757 | // send write-normal-data command (9 bytes) to controller
|
---|
758 | outb(0x03f5, 0xc5); // c5: write normal data
|
---|
759 | outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
|
---|
760 | outb(0x03f5, track);
|
---|
761 | outb(0x03f5, head);
|
---|
762 | outb(0x03f5, sector);
|
---|
763 | outb(0x03f5, 2); // 512 byte sector size
|
---|
764 | outb(0x03f5, sector + num_sectors - 1); // last sector to write on track
|
---|
765 | outb(0x03f5, 0); // Gap length
|
---|
766 | outb(0x03f5, 0xff); // Gap length
|
---|
767 |
|
---|
768 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
769 | // turn on interrupts
|
---|
770 | int_enable();
|
---|
771 |
|
---|
772 | // wait on 40:3e bit 7 to become 1
|
---|
773 | do {
|
---|
774 | val8 = read_byte(0x0040, 0x0040);
|
---|
775 | if (val8 == 0) {
|
---|
776 | floppy_reset_controller(drive);
|
---|
777 | SET_AH(0x80); // drive not ready (timeout)
|
---|
778 | set_diskette_ret_status(0x80);
|
---|
779 | SET_AL(0); // no sectors written
|
---|
780 | SET_CF(); // error occurred
|
---|
781 | return;
|
---|
782 | }
|
---|
783 | val8 = (read_byte(0x0040, 0x003e) & 0x80);
|
---|
784 | } while ( val8 == 0 );
|
---|
785 |
|
---|
786 | val8 = 0; // separate asm from while() loop @todo: why??
|
---|
787 | // turn off interrupts
|
---|
788 | int_disable();
|
---|
789 |
|
---|
790 | // set 40:3e bit 7 to 0
|
---|
791 | val8 = read_byte(0x0040, 0x003e);
|
---|
792 | val8 &= 0x7f;
|
---|
793 | write_byte(0x0040, 0x003e, val8);
|
---|
794 | #else
|
---|
795 | val8 = floppy_wait_for_interrupt_or_timeout();
|
---|
796 | if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
|
---|
797 | floppy_reset_controller(drive);
|
---|
798 | SET_AH(0x80); // drive not ready (timeout)
|
---|
799 | set_diskette_ret_status(0x80);
|
---|
800 | SET_AL(0); // no sectors written
|
---|
801 | SET_CF(); // error occurred
|
---|
802 | return;
|
---|
803 | }
|
---|
804 | #endif
|
---|
805 |
|
---|
806 | // check port 3f4 for accessibility to status bytes
|
---|
807 | val8 = inb(0x3f4);
|
---|
808 | if ( (val8 & 0xc0) != 0xc0 )
|
---|
809 | BX_PANIC("%s: ctrl not ready\n", __func__);
|
---|
810 |
|
---|
811 | // read 7 return status bytes from controller and store in BDA
|
---|
812 | for (i = 0; i < 7; ++i) {
|
---|
813 | return_status[i] = inb(0x3f5);
|
---|
814 | write_byte(0x0040, 0x0042 + i, return_status[i]);
|
---|
815 | }
|
---|
816 |
|
---|
817 | if ( (return_status[0] & 0xc0) != 0 ) {
|
---|
818 | if ( (return_status[1] & 0x02) != 0 ) {
|
---|
819 | // diskette not writable.
|
---|
820 | // AH=status code=0x03 (tried to write on write-protected disk)
|
---|
821 | // AL=number of sectors written=0
|
---|
822 | AX = 0x0300;
|
---|
823 | } else {
|
---|
824 | // Some other problem occurred.
|
---|
825 | AX = 0x0100;
|
---|
826 | }
|
---|
827 | SET_CF();
|
---|
828 | return;
|
---|
829 | }
|
---|
830 |
|
---|
831 | // ??? should track be new val from return_status[3] ?
|
---|
832 | set_diskette_current_cyl(drive, track);
|
---|
833 | // AL = number of sectors read (same value as passed)
|
---|
834 | SET_AH(0x00); // success
|
---|
835 | CLEAR_CF(); // success
|
---|
836 | return;
|
---|
837 | } else { // if (ah == 0x04)
|
---|
838 | // Verify Diskette Sectors
|
---|
839 |
|
---|
840 | // ??? should track be new val from return_status[3] ?
|
---|
841 | set_diskette_current_cyl(drive, track);
|
---|
842 | // AL = number of sectors verified (same value as passed)
|
---|
843 | CLEAR_CF(); // success
|
---|
844 | SET_AH(0x00); // success
|
---|
845 | return;
|
---|
846 | }
|
---|
847 | break;
|
---|
848 |
|
---|
849 | case 0x05: // format diskette track
|
---|
850 | BX_DEBUG_INT13_FL("floppy f05\n");
|
---|
851 |
|
---|
852 | num_sectors = GET_AL();
|
---|
853 | track = GET_CH();
|
---|
854 | head = GET_DH();
|
---|
855 | drive = GET_ELDL();
|
---|
856 |
|
---|
857 | if ((drive > 1) || (head > 1) || (track > 79) ||
|
---|
858 | (num_sectors == 0) || (num_sectors > 18)) {
|
---|
859 | SET_AH(1);
|
---|
860 | set_diskette_ret_status(1);
|
---|
861 | SET_CF(); // error occurred
|
---|
862 | }
|
---|
863 |
|
---|
864 | // see if drive exists
|
---|
865 | if (floppy_drive_exists(drive) == 0) {
|
---|
866 | SET_AH(0x80); // drive not responding
|
---|
867 | set_diskette_ret_status(0x80);
|
---|
868 | SET_CF(); // error occurred
|
---|
869 | return;
|
---|
870 | }
|
---|
871 |
|
---|
872 | // see if media in drive, and type is known
|
---|
873 | if (floppy_media_known(drive) == 0) {
|
---|
874 | if (floppy_media_sense(drive) == 0) {
|
---|
875 | SET_AH(0x0C); // Media type not found
|
---|
876 | set_diskette_ret_status(0x0C);
|
---|
877 | SET_AL(0); // no sectors read
|
---|
878 | SET_CF(); // error occurred
|
---|
879 | return;
|
---|
880 | }
|
---|
881 | }
|
---|
882 |
|
---|
883 | // set up DMA controller for transfer
|
---|
884 | // @todo: merge/factor out pointer normalization
|
---|
885 | page = (ES >> 12); // upper 4 bits
|
---|
886 | base_es = (ES << 4); // lower 16bits contributed by ES
|
---|
887 | base_address = base_es + BX; // lower 16 bits of address
|
---|
888 | // contributed by ES:BX
|
---|
889 | if ( base_address < base_es ) {
|
---|
890 | // in case of carry, adjust page by 1
|
---|
891 | page++;
|
---|
892 | }
|
---|
893 | base_count = (num_sectors * 4) - 1;
|
---|
894 |
|
---|
895 | // check for 64K boundary overrun
|
---|
896 | last_addr = base_address + base_count;
|
---|
897 | if (last_addr < base_address) {
|
---|
898 | SET_AH(0x09);
|
---|
899 | set_diskette_ret_status(0x09);
|
---|
900 | SET_AL(0); // no sectors read
|
---|
901 | SET_CF(); // error occurred
|
---|
902 | return;
|
---|
903 | }
|
---|
904 |
|
---|
905 | outb(0x000a, 0x06);
|
---|
906 | outb(0x000c, 0x00); // clear flip-flop
|
---|
907 | outb(0x0004, base_address);
|
---|
908 | outb(0x0004, base_address>>8);
|
---|
909 | outb(0x000c, 0x00); // clear flip-flop
|
---|
910 | outb(0x0005, base_count);
|
---|
911 | outb(0x0005, base_count>>8);
|
---|
912 | mode_register = 0x4a; // single mode, increment, autoinit disable,
|
---|
913 | // transfer type=read, channel 2
|
---|
914 | outb(0x000b, mode_register);
|
---|
915 | // port 81: DMA-1 Page Register, channel 2
|
---|
916 | outb(0x0081, page);
|
---|
917 | outb(0x000a, 0x02);
|
---|
918 |
|
---|
919 | // set up floppy controller for transfer
|
---|
920 | floppy_prepare_controller(drive);
|
---|
921 |
|
---|
922 | // send seek command to controller
|
---|
923 | outb(0x03f5, 0x0f); // 0f: seek
|
---|
924 | outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
|
---|
925 | outb(0x03f5, track);
|
---|
926 |
|
---|
927 | // send format-track command (6 bytes) to controller
|
---|
928 | outb(0x03f5, 0x4d); // 4d: format track
|
---|
929 | outb(0x03f5, (head << 2) | drive); // HD DR1 DR2
|
---|
930 | outb(0x03f5, 2); // 512 byte sector size
|
---|
931 | outb(0x03f5, num_sectors); // number of sectors per track
|
---|
932 | outb(0x03f5, 0); // Gap length
|
---|
933 | outb(0x03f5, 0xf6); // Fill byte
|
---|
934 |
|
---|
935 | #ifdef VBOX_WITH_FLOPPY_IRQ_POLLING
|
---|
936 | // turn on interrupts
|
---|
937 | int_enable();
|
---|
938 |
|
---|
939 | // wait on 40:3e bit 7 to become 1
|
---|
940 | do {
|
---|
941 | val8 = read_byte(0x0040, 0x0040);
|
---|
942 | if (val8 == 0) {
|
---|
943 | floppy_reset_controller(drive);
|
---|
944 | SET_AH(0x80); // drive not ready (timeout)
|
---|
945 | set_diskette_ret_status(0x80);
|
---|
946 | SET_CF(); // error occurred
|
---|
947 | return;
|
---|
948 | }
|
---|
949 | val8 = (read_byte(0x0040, 0x003e) & 0x80);
|
---|
950 | } while ( val8 == 0 );
|
---|
951 |
|
---|
952 | val8 = 0; // separate asm from while() loop
|
---|
953 | // turn off interrupts
|
---|
954 | int_disable();
|
---|
955 |
|
---|
956 | // set 40:3e bit 7 to 0
|
---|
957 | val8 = read_byte(0x0040, 0x003e);
|
---|
958 | val8 &= 0x7f;
|
---|
959 | write_byte(0x0040, 0x003e, val8);
|
---|
960 | #else
|
---|
961 | val8 = floppy_wait_for_interrupt_or_timeout();
|
---|
962 | if (val8 == 0) { /* Note! Interrupts enabled in this branch. */
|
---|
963 | floppy_reset_controller(drive);
|
---|
964 | SET_AH(0x80); // drive not ready (timeout)
|
---|
965 | set_diskette_ret_status(0x80);
|
---|
966 | SET_CF(); // error occurred
|
---|
967 | return;
|
---|
968 | }
|
---|
969 | #endif
|
---|
970 |
|
---|
971 | // check port 3f4 for accessibility to status bytes
|
---|
972 | val8 = inb(0x3f4);
|
---|
973 | if ( (val8 & 0xc0) != 0xc0 )
|
---|
974 | BX_PANIC("%s: ctrl not ready\n", __func__);
|
---|
975 |
|
---|
976 | // read 7 return status bytes from controller and store in BDA
|
---|
977 | for (i = 0; i < 7; ++i) {
|
---|
978 | return_status[i] = inb(0x3f5);
|
---|
979 | write_byte(0x0040, 0x0042 + i, return_status[i]);
|
---|
980 | }
|
---|
981 |
|
---|
982 | if ( (return_status[0] & 0xc0) != 0 ) {
|
---|
983 | if ( (return_status[1] & 0x02) != 0 ) {
|
---|
984 | // diskette not writable.
|
---|
985 | // AH=status code=0x03 (tried to write on write-protected disk)
|
---|
986 | // AL=number of sectors written=0
|
---|
987 | AX = 0x0300;
|
---|
988 | SET_CF();
|
---|
989 | return;
|
---|
990 | } else {
|
---|
991 | BX_PANIC("%s: write error\n", __func__);
|
---|
992 | }
|
---|
993 | }
|
---|
994 |
|
---|
995 | SET_AH(0);
|
---|
996 | set_diskette_ret_status(0);
|
---|
997 | set_diskette_current_cyl(drive, 0);
|
---|
998 | CLEAR_CF(); // successful
|
---|
999 | return;
|
---|
1000 |
|
---|
1001 |
|
---|
1002 | case 0x08: // read diskette drive parameters
|
---|
1003 | BX_DEBUG_INT13_FL("floppy f08\n");
|
---|
1004 | drive = GET_ELDL();
|
---|
1005 |
|
---|
1006 | if (drive > 1) {
|
---|
1007 | AX = 0;
|
---|
1008 | BX = 0;
|
---|
1009 | CX = 0;
|
---|
1010 | DX = 0;
|
---|
1011 | ES = 0;
|
---|
1012 | DI = 0;
|
---|
1013 | SET_DL(num_floppies);
|
---|
1014 | SET_CF();
|
---|
1015 | return;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | // @todo: break out drive type determination
|
---|
1019 | drive_type = inb_cmos(0x10);
|
---|
1020 | num_floppies = 0;
|
---|
1021 | if (drive_type & 0xf0)
|
---|
1022 | num_floppies++;
|
---|
1023 | if (drive_type & 0x0f)
|
---|
1024 | num_floppies++;
|
---|
1025 |
|
---|
1026 | if (drive == 0)
|
---|
1027 | drive_type >>= 4;
|
---|
1028 | else
|
---|
1029 | drive_type &= 0x0f;
|
---|
1030 |
|
---|
1031 | SET_BH(0);
|
---|
1032 | SET_BL(drive_type);
|
---|
1033 | SET_AH(0);
|
---|
1034 | SET_AL(0);
|
---|
1035 | SET_DL(num_floppies);
|
---|
1036 | SET_DH(1); // max head #
|
---|
1037 |
|
---|
1038 | switch (drive_type) {
|
---|
1039 | case 0: // none
|
---|
1040 | CX = 0;
|
---|
1041 | SET_DH(0); // max head #
|
---|
1042 | break;
|
---|
1043 |
|
---|
1044 | case 1: // 360KB, 5.25"
|
---|
1045 | CX = 0x2709; // 40 tracks, 9 sectors
|
---|
1046 | break;
|
---|
1047 |
|
---|
1048 | case 2: // 1.2MB, 5.25"
|
---|
1049 | CX = 0x4f0f; // 80 tracks, 15 sectors
|
---|
1050 | break;
|
---|
1051 |
|
---|
1052 | case 3: // 720KB, 3.5"
|
---|
1053 | CX = 0x4f09; // 80 tracks, 9 sectors
|
---|
1054 | break;
|
---|
1055 |
|
---|
1056 | case 4: // 1.44MB, 3.5"
|
---|
1057 | CX = 0x4f12; // 80 tracks, 18 sectors
|
---|
1058 | break;
|
---|
1059 |
|
---|
1060 | case 5: // 2.88MB, 3.5"
|
---|
1061 | CX = 0x4f24; // 80 tracks, 36 sectors
|
---|
1062 | break;
|
---|
1063 |
|
---|
1064 | case 14: // 15.6 MB 3.5" (fake)
|
---|
1065 | CX = 0xfe3f; // 255 tracks, 63 sectors
|
---|
1066 | break;
|
---|
1067 |
|
---|
1068 | case 15: // 63.5 MB 3.5" (fake)
|
---|
1069 | CX = 0xfeff; // 255 tracks, 255 sectors - This works because the cylinder
|
---|
1070 | break; // and sectors limits/encoding aren't checked by the BIOS
|
---|
1071 | // due to copy protection schemes and such stuff.
|
---|
1072 |
|
---|
1073 | default: // ?
|
---|
1074 | BX_PANIC("%s: bad floppy type\n", __func__);
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /* set es & di to point to 11 byte diskette param table in ROM */
|
---|
1078 | ES = 0xF000; // @todo: any way to make this relocatable?
|
---|
1079 | DI = get_floppy_dpt(drive_type);
|
---|
1080 | CLEAR_CF(); // success
|
---|
1081 | /* disk status not changed upon success */
|
---|
1082 | return;
|
---|
1083 |
|
---|
1084 | case 0x15: // read diskette drive type
|
---|
1085 | BX_DEBUG_INT13_FL("floppy f15\n");
|
---|
1086 | drive = GET_ELDL();
|
---|
1087 | if (drive > 1) {
|
---|
1088 | SET_AH(0); // only 2 drives supported
|
---|
1089 | // set_diskette_ret_status here ???
|
---|
1090 | SET_CF();
|
---|
1091 | return;
|
---|
1092 | }
|
---|
1093 | // @todo: break out drive type determination
|
---|
1094 | drive_type = inb_cmos(0x10);
|
---|
1095 | if (drive == 0)
|
---|
1096 | drive_type >>= 4;
|
---|
1097 | else
|
---|
1098 | drive_type &= 0x0f;
|
---|
1099 | CLEAR_CF(); // successful, not present
|
---|
1100 | if (drive_type==0) {
|
---|
1101 | SET_AH(0); // drive not present
|
---|
1102 | } else if (drive_type > 1) {
|
---|
1103 | SET_AH(2); // drive present, supports change line
|
---|
1104 | } else {
|
---|
1105 | SET_AH(1); // drive present, does not support change line
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | return;
|
---|
1109 |
|
---|
1110 | case 0x16: // get diskette change line status
|
---|
1111 | BX_DEBUG_INT13_FL("floppy f16\n");
|
---|
1112 | drive = GET_ELDL();
|
---|
1113 | if (drive > 1) {
|
---|
1114 | SET_AH(0x01); // invalid drive
|
---|
1115 | set_diskette_ret_status(0x01);
|
---|
1116 | SET_CF();
|
---|
1117 | return;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | SET_AH(0x06); // change line not supported
|
---|
1121 | set_diskette_ret_status(0x06);
|
---|
1122 | SET_CF();
|
---|
1123 | return;
|
---|
1124 |
|
---|
1125 | case 0x17: // set diskette type for format(old)
|
---|
1126 | BX_DEBUG_INT13_FL("floppy f17\n");
|
---|
1127 | // NOTE: 1.44M diskette not supported by this function, use INT14h/18h instead.
|
---|
1128 | // Drive number (0 or 1) values allowed
|
---|
1129 | drive = GET_ELDL();
|
---|
1130 |
|
---|
1131 | // Format type (AL)
|
---|
1132 | // 00 - NOT USED
|
---|
1133 | // 01 - DISKETTE 360K IN 360K DRIVE
|
---|
1134 | // 02 - DISKETTE 360K IN 1.2M DRIVE
|
---|
1135 | // 03 - DISKETTE 1.2M IN 1.2M DRIVE
|
---|
1136 | // 04 - DISKETTE 720K IN 720K DRIVE
|
---|
1137 | val8 = GET_AL();
|
---|
1138 |
|
---|
1139 | BX_DEBUG_INT13_FL("floppy f17 - drive: %d, format type: %d\n", drive, val8);
|
---|
1140 |
|
---|
1141 | if (drive > 1) {
|
---|
1142 | SET_AH(0x01); // invalid drive
|
---|
1143 | set_diskette_ret_status(0x01); // bad parameter
|
---|
1144 | SET_CF();
|
---|
1145 | return;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | // see if drive exists
|
---|
1149 | if (floppy_drive_exists(drive) == 0) {
|
---|
1150 | SET_AH(0x80); // not responding/time out
|
---|
1151 | set_diskette_ret_status(0x80);
|
---|
1152 | SET_CF();
|
---|
1153 | return;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | // Get current drive state. Set 'base_address' to media status offset address
|
---|
1157 | base_address = (drive) ? 0x0091 : 0x0090;
|
---|
1158 | media_state = read_byte(0x0040, base_address);
|
---|
1159 |
|
---|
1160 | // Mask out (clear) bits 4-7 (4:media type established, 5:double stepping, 6-7:data rate)
|
---|
1161 | media_state &= 0x0f;
|
---|
1162 |
|
---|
1163 | switch (val8) {
|
---|
1164 | case 1:
|
---|
1165 | // 360K media in 360K drive
|
---|
1166 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1167 | break;
|
---|
1168 | case 2:
|
---|
1169 | // 360K media in 1.2M drive
|
---|
1170 | media_state |= 0x70; // 0111 0000 (media type established, double stepping, 300 kbps)
|
---|
1171 | break;
|
---|
1172 | case 3:
|
---|
1173 | // 1.2M media in 1.2M drive
|
---|
1174 | media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
|
---|
1175 | break;
|
---|
1176 | case 4:
|
---|
1177 | // 720K media in 720K drive
|
---|
1178 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1179 | break;
|
---|
1180 | default:
|
---|
1181 | // bad parameter
|
---|
1182 | SET_AH(0x01); // invalid format mode parameter
|
---|
1183 | set_diskette_ret_status(0x01);
|
---|
1184 | SET_CF();
|
---|
1185 | return;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | // Update media status
|
---|
1189 | write_byte(0x0040, base_address, media_state);
|
---|
1190 | BX_DEBUG_INT13_FL("floppy f17 - media status set to: %02x\n", media_state);
|
---|
1191 |
|
---|
1192 | // return success!
|
---|
1193 | SET_AH(0);
|
---|
1194 | set_diskette_ret_status(0);
|
---|
1195 | CLEAR_CF();
|
---|
1196 | return;
|
---|
1197 |
|
---|
1198 | case 0x18: // set diskette type for format(new)
|
---|
1199 | BX_DEBUG_INT13_FL("floppy f18\n");
|
---|
1200 | // Set Media Type for Format. Verifies that the device supports a specific geometry.
|
---|
1201 | // Unlike INT13h/17h, this service supports higher capacity drives (1.44M and 2.88M).
|
---|
1202 | // Drive number (0 or 1) values allowed
|
---|
1203 | drive = GET_ELDL();
|
---|
1204 |
|
---|
1205 | val8 = GET_CL();
|
---|
1206 | num_sectors = val8 & 0x3f; // max sector number per cylinder
|
---|
1207 | track = ((val8 >> 6) << 8) + GET_CH(); // max cylinder number (max cylinders - 1)
|
---|
1208 |
|
---|
1209 | BX_DEBUG_INT13_FL("floppy f18 - drive: %d, max cylinder/track number: %d, sectors-per-tracks: %d\n",
|
---|
1210 | drive, track, num_sectors);
|
---|
1211 |
|
---|
1212 | if (drive > 1) {
|
---|
1213 | SET_AH(0x01); // invalid drive
|
---|
1214 | set_diskette_ret_status(0x01);
|
---|
1215 | SET_CF();
|
---|
1216 | return;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | // see if drive exists
|
---|
1220 | if (floppy_drive_exists(drive) == 0) {
|
---|
1221 | SET_AH(0x80); // not responding/time out
|
---|
1222 | set_diskette_ret_status(0x80);
|
---|
1223 | SET_CF();
|
---|
1224 | return;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | // see if media in drive, and media type is known
|
---|
1228 | if (floppy_media_known(drive) == 0) {
|
---|
1229 | if (floppy_media_sense(drive) == 0) {
|
---|
1230 | SET_AH(0x0C); // drive/media type unknown
|
---|
1231 | set_diskette_ret_status(0x0C);
|
---|
1232 | SET_CF();
|
---|
1233 | return;
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | // @todo: break out drive type determination
|
---|
1238 | drive_type = inb_cmos(0x10);
|
---|
1239 | if (drive == 0)
|
---|
1240 | drive_type >>= 4;
|
---|
1241 | else
|
---|
1242 | drive_type &= 0x0f;
|
---|
1243 |
|
---|
1244 | // Get current drive state. Set 'base_address' to media status offset address
|
---|
1245 | base_address = (drive) ? 0x0091 : 0x0090;
|
---|
1246 | media_state = read_byte(0x0040, base_address);
|
---|
1247 |
|
---|
1248 | // Mask out (clear) bits 4-7 (4:media type established, 5:double stepping, 6-7:data rate)
|
---|
1249 | media_state &= 0x0f;
|
---|
1250 |
|
---|
1251 | switch (drive_type) {
|
---|
1252 | case 1: // 360KB, 5.25"
|
---|
1253 | if (track == 39 && num_sectors == 9)
|
---|
1254 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1255 |
|
---|
1256 | break;
|
---|
1257 | case 2: // 1.2MB, 5.25"
|
---|
1258 | if (track == 39 && num_sectors == 9) { // 360K disk in 1.2M drive
|
---|
1259 | media_state |= 0x70; // 0111 0000 (media type established, double stepping, 300 kbps)
|
---|
1260 | } else if (track == 79 && num_sectors == 15) { // 1.2M disk in 1.2M drive
|
---|
1261 | media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
|
---|
1262 | }
|
---|
1263 | break;
|
---|
1264 | case 3: // 720KB, 3.5"
|
---|
1265 | if (track == 79 && num_sectors == 9)
|
---|
1266 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1267 |
|
---|
1268 | break;
|
---|
1269 | case 4: // 1.44MB, 3.5"
|
---|
1270 | if (track == 79) {
|
---|
1271 | if (num_sectors == 9) { // 720K disk in 1.44M drive
|
---|
1272 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1273 | } else if (num_sectors == 18) { // 1.44M disk in 1.44M drive
|
---|
1274 | media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 | break;
|
---|
1278 | case 5: // 2.88MB, 3.5"
|
---|
1279 | if (track == 79) {
|
---|
1280 | if (num_sectors == 9) { // 720K disk in 2.88M drive
|
---|
1281 | media_state |= 0x90; // 1001 0000 (media type established, 250 kbps)
|
---|
1282 | } else if (num_sectors == 18) { // 1.44M disk in 2.88M drive
|
---|
1283 | media_state |= 0x10; // 0001 0000 (media type established, 500 kbps)
|
---|
1284 | } else if (num_sectors == 36) { // 2.88M disk in 2.88M drive
|
---|
1285 | media_state |= 0xD0; // 1101 0000 (media type established, 1 Mbps)
|
---|
1286 | }
|
---|
1287 | }
|
---|
1288 | break;
|
---|
1289 | default:
|
---|
1290 | break;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | // Error if bit 4 (media type established) has not just been set above.
|
---|
1294 | if (((media_state >> 4) & 0x01) == 0) {
|
---|
1295 | // Error - assume requested tracks/sectors-per-track not supported
|
---|
1296 | // for current drive type - or drive type is unknown!
|
---|
1297 | SET_AH(0x0C);
|
---|
1298 | set_diskette_ret_status(0x0C);
|
---|
1299 | SET_CF();
|
---|
1300 | return;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | // Update media status
|
---|
1304 | write_byte(0x0040, base_address, media_state);
|
---|
1305 |
|
---|
1306 | // set es & di to point to 11 byte diskette param table in ROM
|
---|
1307 | ES = 0xF000; // @todo: any way to make this relocatable?
|
---|
1308 | DI = get_floppy_dpt(drive_type);
|
---|
1309 |
|
---|
1310 | // return success!
|
---|
1311 | SET_AH(0);
|
---|
1312 | set_diskette_ret_status(0);
|
---|
1313 | CLEAR_CF();
|
---|
1314 | return;
|
---|
1315 |
|
---|
1316 | default:
|
---|
1317 | BX_INFO("%s: unsupported AH=%02x\n", __func__, GET_AH());
|
---|
1318 |
|
---|
1319 | // if ( (ah==0x20) || ((ah>=0x41) && (ah<=0x49)) || (ah==0x4e) ) {
|
---|
1320 | SET_AH(0x01); // ???
|
---|
1321 | set_diskette_ret_status(1);
|
---|
1322 | SET_CF();
|
---|
1323 | return;
|
---|
1324 | // }
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | #else // #if BX_SUPPORT_FLOPPY
|
---|
1329 |
|
---|
1330 | void BIOSCALL int13_diskette_function(disk_regs_t r)
|
---|
1331 | {
|
---|
1332 | uint8_t val8;
|
---|
1333 |
|
---|
1334 | switch ( GET_AH() ) {
|
---|
1335 |
|
---|
1336 | case 0x01: // Read Diskette Status
|
---|
1337 | CLEAR_CF();
|
---|
1338 | val8 = read_byte(0x0000, 0x0441);
|
---|
1339 | SET_AH(val8);
|
---|
1340 | if (val8) {
|
---|
1341 | SET_CF();
|
---|
1342 | }
|
---|
1343 | return;
|
---|
1344 |
|
---|
1345 | default:
|
---|
1346 | SET_CF();
|
---|
1347 | write_byte(0x0000, 0x0441, 0x01);
|
---|
1348 | SET_AH(0x01);
|
---|
1349 | }
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | #endif // #if BX_SUPPORT_FLOPPY
|
---|
1353 |
|
---|
1354 | #if 0
|
---|
1355 | void determine_floppy_media(uint16_t drive)
|
---|
1356 | {
|
---|
1357 | uint8_t val8, DOR, ctrl_info;
|
---|
1358 |
|
---|
1359 | ctrl_info = read_byte(0x0040, 0x008F);
|
---|
1360 | if (drive==1)
|
---|
1361 | ctrl_info >>= 4;
|
---|
1362 | else
|
---|
1363 | ctrl_info &= 0x0f;
|
---|
1364 |
|
---|
1365 | #if 0
|
---|
1366 | if (drive == 0) {
|
---|
1367 | DOR = 0x1c; // DOR: drive0 motor on, DMA&int enabled, normal op, drive select 0
|
---|
1368 | }
|
---|
1369 | else {
|
---|
1370 | DOR = 0x2d; // DOR: drive1 motor on, DMA&int enabled, normal op, drive select 1
|
---|
1371 | }
|
---|
1372 | #endif
|
---|
1373 |
|
---|
1374 | if ( (ctrl_info & 0x04) != 0x04 ) {
|
---|
1375 | // Drive not determined means no drive exists, done.
|
---|
1376 | return;
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | #if 0
|
---|
1380 | // check Main Status Register for readiness
|
---|
1381 | val8 = inb(0x03f4) & 0x80; // Main Status Register
|
---|
1382 | if (val8 != 0x80)
|
---|
1383 | BX_PANIC("d_f_m: MRQ bit not set\n");
|
---|
1384 |
|
---|
1385 | // change line
|
---|
1386 |
|
---|
1387 | // existing BDA values
|
---|
1388 |
|
---|
1389 | // turn on drive motor
|
---|
1390 | outb(0x03f2, DOR); // Digital Output Register
|
---|
1391 | //
|
---|
1392 | #endif
|
---|
1393 | BX_PANIC("d_f_m: OK so far\n");
|
---|
1394 | }
|
---|
1395 | #endif
|
---|
1396 |
|
---|