floppy.c revision 94fde0b66aced35ee31b2218ab4371f2a48888c1
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
* --------------------------------------------------------------------
*
* This code is based on:
*
*
* Copyright (C) 2002 MandrakeSoft S.A.
*
* MandrakeSoft S.A.
* 43, rue d'Aboukir
* 75002 Paris - France
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdint.h>
#include "inlines.h"
#include "biosint.h"
//////////////////////
// FLOPPY functions //
//////////////////////
{
}
{
if (drive > 1)
BX_PANIC("set_diskette_current_cyl: drive > 1\n");
}
#if 1 //BX_SUPPORT_FLOPPY
#if DEBUG_INT13_FL
#else
# define BX_DEBUG_INT13_FL(...)
#endif
extern int diskette_param_table; /* At a fixed location. */
void floppy_reset_controller(void)
{
// Reset controller
// Wait for controller to come out of reset
do {
}
{
// set 40:3e bit 7 to 0
val8 &= 0x7f;
// turn on motor of selected drive, DMA & int enabled, normal operation
if (drive)
dor = 0x20;
else
dor = 0x10;
dor |= 0x0c;
// reset the disk motor timeout value of INT 08
// program data rate
val8 >>= 6;
// wait for drive readiness
do {
if (prev_reset == 0) {
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
} while ( (val8 & 0x80) == 0 );
val8 &= 0x7f;
int_disable();
}
}
{
if (drive)
val8 >>= 1;
val8 &= 0x01;
if (val8 == 0)
return 0;
media_state_offset = 0x0090;
if (drive)
media_state_offset += 1;
if (val8 == 0)
return 0;
// checks passed, return KNOWN
return 1;
}
{
int i;
// send Read ID command (2 bytes) to controller
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
} while ( val8 == 0 );
val8 = 0; // separate asm from while() loop
// turn off interrupts
int_disable();
// read 7 return status bytes from controller
for (i = 0; i < 7; ++i) {
}
if ( (return_status[0] & 0xc0) != 0 )
return 0;
else
return 1;
}
{
// send Recalibrate command (2 bytes) to controller
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
} while ( val8 == 0 );
val8 = 0; // separate asm from while() loop
// turn off interrupts
int_disable();
// set 40:3e bit 7 to 0, and calibrated bit
val8 &= 0x7f;
if (drive) {
curr_cyl_offset = 0x0095;
} else {
curr_cyl_offset = 0x0094;
}
return 1;
}
{
if (floppy_drive_recal(drive) == 0)
return 0;
// Try the diskette data rates in the following order:
// 1 Mbps -> 500 Kbps -> 300 Kbps -> 250 Kbps
// The 1 Mbps rate is only tried for 2.88M drives.
// ** config_data **
// Bitfields for diskette media control:
// Bit(s) Description (Table M0028)
// 7-6 last data rate set by controller
// 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
// 5-4 last diskette drive step rate selected
// 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
// 3-2 {data rate at start of operation}
// 1-0 reserved
// ** media_state **
// Bitfields for diskette drive media state:
// Bit(s) Description (Table M0030)
// 7-6 data rate
// 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
// 5 double stepping required (e.g. 360kB in 1.2MB)
// 4 media type established
// 3 drive capable of supporting 4MB media
// 2-0 on exit from BIOS, contains
// 000 trying 360kB in 360kB
// 001 trying 360kB in 1.2MB
// 010 trying 1.2MB in 1.2MB
// 011 360kB in 360kB established
// 100 360kB in 1.2MB established
// 101 1.2MB in 1.2MB established
// 110 reserved
// @todo: break out drive type determination
if (drive == 0)
drive_type >>= 4;
else
drive_type &= 0x0f;
if ( drive_type == 1 ) {
// 360K 5.25" drive
retval = 1;
}
else if ( drive_type == 2 ) {
// 1.2 MB 5.25" drive
retval = 1;
}
else if ( drive_type == 3 ) {
// 720K 3.5" drive
retval = 1;
}
else if ( drive_type == 4 ) {
// 1.44 MB 3.5" drive
retval = 1;
}
else if ( drive_type == 5 ) {
// 2.88 MB 3.5" drive
retval = 1;
}
// Extended floppy size uses special cmos setting
else if ( drive_type == 6 ) {
// 160k 5.25" drive
retval = 1;
}
else if ( drive_type == 7 ) {
// 180k 5.25" drive
retval = 1;
}
else if ( drive_type == 8 ) {
// 320k 5.25" drive
retval = 1;
}
else {
// not recognized
retval = 0;
}
while (!floppy_read_id(drive)) {
// If even 250 Kbps failed, we can't do much
break;
}
switch (config_data & 0xC0) {
case 0xC0: // 1 Mbps
break;
case 0x00: // 500 Kbps
break;
case 0x40: // 300 Kbps
break;
}
}
if (drive == 0)
media_state_offset = 0x0090;
else
media_state_offset = 0x0091;
return retval;
}
{
// check CMOS to see if drive exists
// @todo: break out drive type determination
if (drive == 0)
drive_type >>= 4;
else
drive_type &= 0x0f;
return drive_type != 0;
}
//@todo: put in a header
{
int i;
switch ( ah ) {
case 0x00: // diskette controller reset
BX_DEBUG_INT13_FL("floppy f00\n");
if (drive > 1) {
SET_CF();
return;
}
// @todo: break out drive type determination
if (drive == 0)
drive_type >>= 4;
else
drive_type &= 0x0f;
if (drive_type == 0) {
set_diskette_ret_status(0x80);
SET_CF();
return;
}
// force re-calibration etc.
SET_AH(0);
CLEAR_CF(); // successful
return;
case 0x01: // Read Diskette Status
CLEAR_CF();
if (val8) {
SET_CF();
}
return;
case 0x02: // Read Diskette Sectors
case 0x03: // Write Diskette Sectors
case 0x04: // Verify Diskette Sectors
num_sectors = GET_AL();
SET_AH(1);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
// see if drive exists
if (floppy_drive_exists(drive) == 0) {
set_diskette_ret_status(0x80);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
// see if media in drive, and type is known
if (floppy_media_known(drive) == 0) {
if (floppy_media_sense(drive) == 0) {
set_diskette_ret_status(0x0C);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
}
if (ah == 0x02) {
// Read Diskette Sectors
//-----------------------------------
// set up DMA controller for transfer
//-----------------------------------
// es:bx = pointer to where to place information from diskette
// port 04: DMA-1 base and current address, channel 2
// port 05: DMA-1 base and current count, channel 2
// contributed by ES:BX
if ( base_address < base_es ) {
// in case of carry, adjust page by 1
page++;
}
// check for 64K boundary overrun
if (last_addr < base_address) {
SET_AH(0x09);
set_diskette_ret_status(0x09);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
BX_DEBUG_INT13_FL("clear flip-flop\n");
BX_DEBUG_INT13_FL("clear flip-flop\n");
// port 0b: DMA-1 Mode Register
// transfer type=write, channel 2
BX_DEBUG_INT13_FL("setting mode register\n");
BX_DEBUG_INT13_FL("setting page register\n");
// port 81: DMA-1 Page Register, channel 2
BX_DEBUG_INT13_FL("unmask chan 2\n");
BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
//--------------------------------------
// set up floppy controller for transfer
//--------------------------------------
// send read-normal-data command (9 bytes) to controller
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
if (val8 == 0) {
set_diskette_ret_status(0x80);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
} while ( val8 == 0 );
val8 = 0; // separate asm from while() loop
// turn off interrupts
int_disable();
// set 40:3e bit 7 to 0
val8 &= 0x7f;
// check port 3f4 for accessibility to status bytes
// read 7 return status bytes from controller and store in BDA
for (i = 0; i < 7; ++i) {
}
if ( (return_status[0] & 0xc0) != 0 ) {
SET_AH(0x20);
set_diskette_ret_status(0x20);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
#ifdef DMA_WORKAROUND
#endif
// ??? should track be new val from return_status[3] ?
// AL = number of sectors read (same value as passed)
CLEAR_CF(); // success
return;
} else if (ah == 0x03) {
// Write Diskette Sectors
//-----------------------------------
// set up DMA controller for transfer
//-----------------------------------
// es:bx = pointer to where to place information from diskette
// port 04: DMA-1 base and current address, channel 2
// port 05: DMA-1 base and current count, channel 2
// contributed by ES:BX
if ( base_address < base_es ) {
// in case of carry, adjust page by 1
page++;
}
// check for 64K boundary overrun
if (last_addr < base_address) {
SET_AH(0x09);
set_diskette_ret_status(0x09);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
BX_DEBUG_INT13_FL("masking DMA-1 c2\n");
// port 0b: DMA-1 Mode Register
// transfer type=read, channel 2
// port 81: DMA-1 Page Register, channel 2
BX_DEBUG_INT13_FL("unmasking DMA-1 c2\n");
//--------------------------------------
// set up floppy controller for transfer
//--------------------------------------
// send write-normal-data command (9 bytes) to controller
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
if (val8 == 0) {
set_diskette_ret_status(0x80);
SET_AL(0); // no sectors written
SET_CF(); // error occurred
return;
}
} while ( val8 == 0 );
val8 = 0; // separate asm from while() loop @todo: why??
// turn off interrupts
int_disable();
// set 40:3e bit 7 to 0
val8 &= 0x7f;
// check port 3f4 for accessibility to status bytes
// read 7 return status bytes from controller and store in BDA
for (i = 0; i < 7; ++i) {
}
if ( (return_status[0] & 0xc0) != 0 ) {
// diskette not writable.
// AH=status code=0x03 (tried to write on write-protected disk)
// AL=number of sectors written=0
AX = 0x0300;
SET_CF();
return;
} else {
}
}
// ??? should track be new val from return_status[3] ?
// AL = number of sectors read (same value as passed)
CLEAR_CF(); // success
return;
} else { // if (ah == 0x04)
// Verify Diskette Sectors
// ??? should track be new val from return_status[3] ?
// AL = number of sectors verified (same value as passed)
CLEAR_CF(); // success
return;
}
break;
case 0x05: // format diskette track
BX_DEBUG_INT13_FL("floppy f05\n");
num_sectors = GET_AL();
SET_AH(1);
SET_CF(); // error occurred
}
// see if drive exists
if (floppy_drive_exists(drive) == 0) {
set_diskette_ret_status(0x80);
SET_CF(); // error occurred
return;
}
// see if media in drive, and type is known
if (floppy_media_known(drive) == 0) {
if (floppy_media_sense(drive) == 0) {
set_diskette_ret_status(0x0C);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
}
// set up DMA controller for transfer
// contributed by ES:BX
if ( base_address < base_es ) {
// in case of carry, adjust page by 1
page++;
}
// check for 64K boundary overrun
if (last_addr < base_address) {
SET_AH(0x09);
set_diskette_ret_status(0x09);
SET_AL(0); // no sectors read
SET_CF(); // error occurred
return;
}
// transfer type=read, channel 2
// port 81: DMA-1 Page Register, channel 2
// set up floppy controller for transfer
// send format-track command (6 bytes) to controller
// turn on interrupts
int_enable();
// wait on 40:3e bit 7 to become 1
do {
if (val8 == 0) {
set_diskette_ret_status(0x80);
SET_CF(); // error occurred
return;
}
} while ( val8 == 0 );
val8 = 0; // separate asm from while() loop
// turn off interrupts
int_disable();
// set 40:3e bit 7 to 0
val8 &= 0x7f;
// check port 3f4 for accessibility to status bytes
// read 7 return status bytes from controller and store in BDA
for (i = 0; i < 7; ++i) {
}
if ( (return_status[0] & 0xc0) != 0 ) {
// diskette not writable.
// AH=status code=0x03 (tried to write on write-protected disk)
// AL=number of sectors written=0
AX = 0x0300;
SET_CF();
return;
} else {
}
}
SET_AH(0);
CLEAR_CF(); // successful
return;
case 0x08: // read diskette drive parameters
BX_DEBUG_INT13_FL("floppy f08\n");
if (drive > 1) {
AX = 0;
BX = 0;
CX = 0;
DX = 0;
ES = 0;
DI = 0;
SET_CF();
return;
}
// @todo: break out drive type determination
num_floppies = 0;
if (drive_type & 0xf0)
num_floppies++;
if (drive_type & 0x0f)
num_floppies++;
if (drive == 0)
drive_type >>= 4;
else
drive_type &= 0x0f;
SET_BH(0);
SET_AH(0);
SET_AL(0);
switch (drive_type) {
case 0: // none
CX = 0;
SET_DH(0); // max head #
break;
case 1: // 360KB, 5.25"
break;
case 2: // 1.2MB, 5.25"
break;
case 3: // 720KB, 3.5"
break;
case 4: // 1.44MB, 3.5"
break;
case 5: // 2.88MB, 3.5"
break;
case 6: // 160k, 5.25"
SET_DH(0); // max head #
break;
case 7: // 180k, 5.25"
SET_DH(0); // max head #
break;
case 8: // 320k, 5.25"
break;
default: // ?
}
/* set es & di to point to 11 byte diskette param table in ROM */
CLEAR_CF(); // success
/* disk status not changed upon success */
return;
case 0x15: // read diskette drive type
BX_DEBUG_INT13_FL("floppy f15\n");
if (drive > 1) {
SET_AH(0); // only 2 drives supported
// set_diskette_ret_status here ???
SET_CF();
return;
}
// @todo: break out drive type determination
if (drive == 0)
drive_type >>= 4;
else
drive_type &= 0x0f;
CLEAR_CF(); // successful, not present
if (drive_type==0) {
SET_AH(0); // drive not present
}
else {
}
return;
case 0x16: // get diskette change line status
BX_DEBUG_INT13_FL("floppy f16\n");
if (drive > 1) {
set_diskette_ret_status(0x01);
SET_CF();
return;
}
set_diskette_ret_status(0x06);
SET_CF();
return;
case 0x17: // set diskette type for format(old)
BX_DEBUG_INT13_FL("floppy f17\n");
/* not used for 1.44M floppies */
SET_CF();
return;
case 0x18: // set diskette type for format(new)
BX_DEBUG_INT13_FL("floppy f18\n");
SET_CF();
return;
default:
// if ( (ah==0x20) || ((ah>=0x41) && (ah<=0x49)) || (ah==0x4e) ) {
SET_CF();
return;
// }
}
}
#else // #if BX_SUPPORT_FLOPPY
{
switch ( GET_AH() ) {
case 0x01: // Read Diskette Status
CLEAR_CF();
if (val8) {
SET_CF();
}
return;
default:
SET_CF();
SET_AH(0x01);
}
}
#endif // #if BX_SUPPORT_FLOPPY
#if 0
{
if (drive==1)
ctrl_info >>= 4;
else
ctrl_info &= 0x0f;
#if 0
if (drive == 0) {
}
else {
}
#endif
// Drive not determined means no drive exists, done.
return;
}
#if 0
// check Main Status Register for readiness
if (val8 != 0x80)
BX_PANIC("d_f_m: MRQ bit not set\n");
// change line
// existing BDA values
// turn on drive motor
//
#endif
BX_PANIC("d_f_m: OK so far\n");
}
#endif