⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atapiio.cpp

📁 3sc2443的CF卡IDE源代码,肯定好用.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//     Return interrupt reason/status
//
// Parameters:
//     None
// ----------------------------------------------------------------------------

WORD
CDisk::CheckIntrState(
    )
{
    BYTE bReason, bDRQ;

    WaitOnBusy(FALSE);

    bReason = GetReason() & (ATA_IR_CoD | ATA_IR_IO);
    bDRQ = GetAltStatus() & ATA_STATUS_DATA_REQ;

    if (bDRQ) {
        bReason |= 4;
    }
    if (bReason < 3) {
        return((WORD) ATA_INTR_READY);
    }

    return ((WORD)bReason);
}

// ----------------------------------------------------------------------------
// Function: ReadBuffer
//     Fill buffer from data register
//
// Parameters:
//     pBuffer -
//     dwCount -
// ----------------------------------------------------------------------------

void
CDisk::ReadBuffer(
    PBYTE pBuffer,
    DWORD dwCount
    )
{
    union {
        WORD us;
        BYTE  uc[2];
    } unisc;

    if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STARTREADBUFFER, &dwCount, sizeof(dwCount), 0, CELZONE_ALWAYSON, 0, FALSE);

    if (dwCount == 0) {
        return;
    }

    // determine whether required byte was read in previous read;
    // m_wNextByte=(-1) implies byte not read in previous read

    if (m_wNextByte != 0xFFFF) {
        DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Unaligned buffer on prevous read\r\n")));
        // update first byte
        *pBuffer++ = (BYTE)m_wNextByte;
        dwCount--;
    }

    // check alignment of pBuffer

    if ((DWORD)pBuffer & 1) {
        DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Unaligned buffer\r\n")));
        while (dwCount> 1) {
            unisc.us = ReadWord();
            *pBuffer++= unisc.uc[0];
            *pBuffer++= unisc.uc[1];
            dwCount-=2;
        }
    }
    else {
        ReadWordBuffer((PWORD)pBuffer,(DWORD)(dwCount)/sizeof(SHORT));
        pBuffer += dwCount;
        dwCount &= 1;       // if 1, we need to read the next byte still
        pBuffer -= dwCount; // adjust pBuffer if its address is odd
    }

    // read one word even if we only need one byte; save the unused byte and
    // use it as the first byte of the next read (scatter/gather buffer)

    if (dwCount == 1) {
        DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::ReadBuffer> Reading word for one byte\r\n")));
        unisc.us = ReadWord();
        *pBuffer=   unisc.uc[0];
        m_wNextByte = (WORD)unisc.uc[1]; // save the second byte
    }

    BYTE bStatus = GetAltStatus();

    if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_COMPLETEREADBUFFER, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
}

// ----------------------------------------------------------------------------
// Function: WriteBuffer
//     Empty buffer to data register
//
// Parameters:
//     pBuffer -
//     dwCount -
// ----------------------------------------------------------------------------

void
CDisk::WriteBuffer(
    PBYTE pBuffer,
    DWORD dwCount
    )
{
    union {
        WORD us;
        BYTE  uc[2];
    } unisc;

    if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_STARTWRITEBUFFER, &dwCount, sizeof(dwCount), 0, CELZONE_ALWAYSON, 0, FALSE);

    if (dwCount == 0) {
        return;
    }

    // determine whether required byte was written in previous write;
    // m_wNextByte=(-1) implies byte not written in previous write

    if (m_wNextByte != 0xFFFF) {
        // update first byte
        DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Unaligned buffer on previous write\r\n")));
        unisc.uc[0] = (BYTE) m_wNextByte;
        unisc.uc[1] = *pBuffer++;
        dwCount--; 
        WriteWord(unisc.us);
    }

    // check alignment of pBuffer

    if ((DWORD) pBuffer & 1) {
        DEBUGMSG(ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Unaligned buffer\r\n")));
        while (dwCount> 1) {
            unisc.uc[0] = *pBuffer++;
            unisc.uc[1] = *pBuffer++;
            WriteWord(unisc.us);
            dwCount-=2;
        }
    }
    else {
        WriteWordBuffer((PWORD)pBuffer,(DWORD)(dwCount)/sizeof(SHORT));
        pBuffer += dwCount;
        dwCount &= 1;       // if 1, we need to write the next byte still
        pBuffer -= dwCount; // adjust pBuffer if its address is odd

    }

    // write one word even if we only need one byte; save the unused byte and
    // use it as the first byte of the next read (scatter/gather buffer)

    if (dwCount == 1) {
        DEBUGMSG( ZONE_WARNING, (TEXT("Atapi!CDisk::WriteBuffer> Writing one word for one byte\r\n")));
        m_wNextByte = (WORD) *pBuffer; // save the second byte
    }

    BYTE bStatus = GetAltStatus();

    if (ZONE_CELOG) CeLogData(TRUE, CELID_ATAPI_COMPLETEWRITEBUFFER, &bStatus, sizeof(bStatus), 0, CELZONE_ALWAYSON, 0, FALSE);
}

// ----------------------------------------------------------------------------
// Function: SetTransferMode
//     Set a device's transfer mode through the SET FEATURES command
//
// Parameters:
//     bMode - desired transfer mode
//
// Notes:
//     See ATA/ATAPI-6 R3B 8.15 IDENTIFY DEVICE for more information regarding
//     IDENTIFY DEVICE data.
//     See ATA/ATAPI-6 R3B 8.46 SET FEATURES for more information regarding
//     transfer mode encodings.
// ----------------------------------------------------------------------------

BOOL
CDisk::SetTransferMode(
    BYTE bMode
    )
{
    BYTE bStatus; // Status register
    BYTE bError;  // Error register
    BOOL fOk;     // result

    // HI:Check_Status (Host Idle); wait until BSY=0 and DRQ=0
    // read Status register
    while (1) {
        bStatus = GetAltStatus();
        if (!(bStatus & (0x80|0x08))) break; // BSY := Bit 7, DRQ := Bit 3
        Sleep(5);
    }

    // HI:Device_Select; select device
    SelectDevice();

    // HI:Check_Status (Host Idle); wait until BSY=0 and DRQ=0
    // read Status register
    while (1) {
        bStatus = GetAltStatus();
        if (!(bStatus & (0x80|0x08))) break; // BSY := Bit 7, DRQ := Bit 3
        Sleep(5);
    }

    // HI:Write_Parameters
    WriteFeature(0x03);            // set transfer mode based on value in Sector Count register (Table 44)
    WriteSectorCount(bMode);       // (Table 45)
    WriteAltDriveController(0x00); // disable interrupt (nIEN := Bit 1 of Device Control register)

    // HI:Write_Command
    WriteCommand(0xEF); // SET FEATURES command code := EFh

    // transition to non-data command protocol

    // HND:INTRQ_Wait
    // transition to HND:Check_Status
    // read Status register
    while (1) { // BSY := Bit 7
        bStatus = GetAltStatus();
        bError = GetError();
        if (bError & 0x04) { // ABRT := Bit 2
            // command was aborted
            DEBUGMSG(ZONE_ERROR, (_T(
                "Atapi!CDisk::SetTransferMode> Failed to send SET FEATURES command, parameter 0x%x\r\n"
                ), bMode));
            fOk = FALSE;
            break;
        }
        if (!(bStatus & 0x80)) break; // BSY := Bit 7
        Sleep(5);
    }

    // transition to host idle protocol

    return fOk;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -