static void ide_atapi_cmd_reply_end(IDEState *s)

{

    int byte_count_limit, size, ret;

#ifdef DEBUG_IDE_ATAPI

    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",

           s->packet_transfer_size,

           s->elementary_transfer_size,

           s->io_buffer_index);

#endif

    if (s->packet_transfer_size <= 0) {

        /* end of transfer */

        ide_transfer_stop(s);

        s->status = READY_STAT;

        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;

        ide_set_irq(s);

#ifdef DEBUG_IDE_ATAPI

        printf("status=0x%x\n", s->status);

#endif

    } else {

        /* see if a new sector must be read */

        if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {

            ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);

            if (ret < 0) {

                ide_transfer_stop(s);

                ide_atapi_io_error(s, ret);

                return;

            }

            s->lba++;

            s->io_buffer_index = 0;

        }

        if (s->elementary_transfer_size > 0) {

            /* there are some data left to transmit in this elementary

               transfer */

            size = s->cd_sector_size - s->io_buffer_index;

            if (size > s->elementary_transfer_size)

                size = s->elementary_transfer_size;

            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,

                               size, ide_atapi_cmd_reply_end);

            s->packet_transfer_size -= size;

            s->elementary_transfer_size -= size;

            s->io_buffer_index += size;

        } else {

            /* a new transfer is needed */

            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;

            byte_count_limit = s->lcyl | (s->hcyl << 8);

#ifdef DEBUG_IDE_ATAPI

            printf("byte_count_limit=%d\n", byte_count_limit);

#endif

            if (byte_count_limit == 0xffff)

                byte_count_limit--;

            size = s->packet_transfer_size;

            if (size > byte_count_limit) {

                /* byte count limit must be even if this case */

                if (byte_count_limit & 1)

                    byte_count_limit--;

                size = byte_count_limit;

            }

            s->lcyl = size;

            s->hcyl = size >> 8;

            s->elementary_transfer_size = size;

            /* we cannot transmit more than one sector at a time */

            if (s->lba != -1) {

                if (size > (s->cd_sector_size - s->io_buffer_index))

                    size = (s->cd_sector_size - s->io_buffer_index);

            }

            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,

                               size, ide_atapi_cmd_reply_end);

            s->packet_transfer_size -= size;

            s->elementary_transfer_size -= size;

            s->io_buffer_index += size;

            ide_set_irq(s);

#ifdef DEBUG_IDE_ATAPI

            printf("status=0x%x\n", s->status);

#endif

        }

    }

}

static void ide_atapi_cmd_reply_end(IDEState *s)

{

    int byte_count_limit, size, ret;

#ifdef DEBUG_IDE_ATAPI

    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",

           s->packet_transfer_size,

           s->elementary_transfer_size,

           s->io_buffer_index);

#endif

    if (s->packet_transfer_size <= 0) {

        

        ide_transfer_stop(s);

        s->status = READY_STAT;

        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;

        ide_set_irq(s);

#ifdef DEBUG_IDE_ATAPI

        printf("status=0x%x\n", s->status);

#endif

    } else {

        

        if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {

            ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);

            if (ret < 0) {

                ide_transfer_stop(s);

                ide_atapi_io_error(s, ret);

                return;

            }

            s->lba++;

            s->io_buffer_index = 0;

        }

        if (s->elementary_transfer_size > 0) {

            



            size = s->cd_sector_size - s->io_buffer_index;

            if (size > s->elementary_transfer_size)

                size = s->elementary_transfer_size;

            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,

                               size, ide_atapi_cmd_reply_end);

            s->packet_transfer_size -= size;

            s->elementary_transfer_size -= size;

            s->io_buffer_index += size;

        } else {

            

            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;

            byte_count_limit = s->lcyl | (s->hcyl << 8);

#ifdef DEBUG_IDE_ATAPI

            printf("byte_count_limit=%d\n", byte_count_limit);

#endif

            if (byte_count_limit == 0xffff)

                byte_count_limit--;

            size = s->packet_transfer_size;

            if (size > byte_count_limit) {

                

                if (byte_count_limit & 1)

                    byte_count_limit--;

                size = byte_count_limit;

            }

            s->lcyl = size;

            s->hcyl = size >> 8;

            s->elementary_transfer_size = size;

            

            if (s->lba != -1) {

                if (size > (s->cd_sector_size - s->io_buffer_index))

                    size = (s->cd_sector_size - s->io_buffer_index);

            }

            ide_transfer_start(s, s->io_buffer + s->io_buffer_index,

                               size, ide_atapi_cmd_reply_end);

            s->packet_transfer_size -= size;

            s->elementary_transfer_size -= size;

            s->io_buffer_index += size;

            ide_set_irq(s);

#ifdef DEBUG_IDE_ATAPI

            printf("status=0x%x\n", s->status);

#endif

        }

    }

}

static void FUNC_0(IDEState *VAR_0)

{

    int VAR_1, VAR_2, VAR_3;

#ifdef DEBUG_IDE_ATAPI

    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",

           VAR_0->packet_transfer_size,

           VAR_0->elementary_transfer_size,

           VAR_0->io_buffer_index);

#endif

    if (VAR_0->packet_transfer_size <= 0) {

        

        ide_transfer_stop(VAR_0);

        VAR_0->status = READY_STAT;

        VAR_0->nsector = (VAR_0->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;

        ide_set_irq(VAR_0);

#ifdef DEBUG_IDE_ATAPI

        printf("status=0x%x\n", VAR_0->status);

#endif

    } else {

        

        if (VAR_0->lba != -1 && VAR_0->io_buffer_index >= VAR_0->cd_sector_size) {

            VAR_3 = cd_read_sector(VAR_0->bs, VAR_0->lba, VAR_0->io_buffer, VAR_0->cd_sector_size);

            if (VAR_3 < 0) {

                ide_transfer_stop(VAR_0);

                ide_atapi_io_error(VAR_0, VAR_3);

                return;

            }

            VAR_0->lba++;

            VAR_0->io_buffer_index = 0;

        }

        if (VAR_0->elementary_transfer_size > 0) {

            



            VAR_2 = VAR_0->cd_sector_size - VAR_0->io_buffer_index;

            if (VAR_2 > VAR_0->elementary_transfer_size)

                VAR_2 = VAR_0->elementary_transfer_size;

            ide_transfer_start(VAR_0, VAR_0->io_buffer + VAR_0->io_buffer_index,

                               VAR_2, FUNC_0);

            VAR_0->packet_transfer_size -= VAR_2;

            VAR_0->elementary_transfer_size -= VAR_2;

            VAR_0->io_buffer_index += VAR_2;

        } else {

            

            VAR_0->nsector = (VAR_0->nsector & ~7) | ATAPI_INT_REASON_IO;

            VAR_1 = VAR_0->lcyl | (VAR_0->hcyl << 8);

#ifdef DEBUG_IDE_ATAPI

            printf("VAR_1=%d\n", VAR_1);

#endif

            if (VAR_1 == 0xffff)

                VAR_1--;

            VAR_2 = VAR_0->packet_transfer_size;

            if (VAR_2 > VAR_1) {

                

                if (VAR_1 & 1)

                    VAR_1--;

                VAR_2 = VAR_1;

            }

            VAR_0->lcyl = VAR_2;

            VAR_0->hcyl = VAR_2 >> 8;

            VAR_0->elementary_transfer_size = VAR_2;

            

            if (VAR_0->lba != -1) {

                if (VAR_2 > (VAR_0->cd_sector_size - VAR_0->io_buffer_index))

                    VAR_2 = (VAR_0->cd_sector_size - VAR_0->io_buffer_index);

            }

            ide_transfer_start(VAR_0, VAR_0->io_buffer + VAR_0->io_buffer_index,

                               VAR_2, FUNC_0);

            VAR_0->packet_transfer_size -= VAR_2;

            VAR_0->elementary_transfer_size -= VAR_2;

            VAR_0->io_buffer_index += VAR_2;

            ide_set_irq(VAR_0);

#ifdef DEBUG_IDE_ATAPI

            printf("status=0x%x\n", VAR_0->status);

#endif

        }

    }

}

static void FUNC_0(IDEState *VAR_0)
{ int VAR_1, VAR_2, VAR_3; #ifdef DEBUG_IDE_ATAPI
printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
VAR_0->packet_transfer_size,
VAR_0->elementary_transfer_size,
VAR_0->io_buffer_index); #endif
if (VAR_0->packet_transfer_size <= 0) { ide_transfer_stop(VAR_0); VAR_0->status = READY_STAT; VAR_0->nsector = (VAR_0->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD; ide_set_irq(VAR_0); #ifdef DEBUG_IDE_ATAPI
printf("status=0x%x\n", VAR_0->status); #endif
} else { if (VAR_0->lba != -1 && VAR_0->io_buffer_index >= VAR_0->cd_sector_size) { VAR_3 = cd_read_sector(VAR_0->bs, VAR_0->lba, VAR_0->io_buffer, VAR_0->cd_sector_size); if (VAR_3 < 0) { ide_transfer_stop(VAR_0); ide_atapi_io_error(VAR_0, VAR_3); return; } VAR_0->lba++; VAR_0->io_buffer_index = 0; } if (VAR_0->elementary_transfer_size > 0) { VAR_2 = VAR_0->cd_sector_size - VAR_0->io_buffer_index; if (VAR_2 > VAR_0->elementary_transfer_size)
VAR_2 = VAR_0->elementary_transfer_size; ide_transfer_start(VAR_0, VAR_0->io_buffer + VAR_0->io_buffer_index,
VAR_2, FUNC_0); VAR_0->packet_transfer_size -= VAR_2; VAR_0->elementary_transfer_size -= VAR_2; VAR_0->io_buffer_index += VAR_2; } else { VAR_0->nsector = (VAR_0->nsector & ~7) | ATAPI_INT_REASON_IO; VAR_1 = VAR_0->lcyl | (VAR_0->hcyl << 8); #ifdef DEBUG_IDE_ATAPI
printf("VAR_1=%d\n", VAR_1); #endif
if (VAR_1 == 0xffff)
VAR_1--; VAR_2 = VAR_0->packet_transfer_size; if (VAR_2 > VAR_1) { if (VAR_1 & 1)
VAR_1--; VAR_2 = VAR_1; } VAR_0->lcyl = VAR_2; VAR_0->hcyl = VAR_2 >> 8; VAR_0->elementary_transfer_size = VAR_2; if (VAR_0->lba != -1) { if (VAR_2 > (VAR_0->cd_sector_size - VAR_0->io_buffer_index))
VAR_2 = (VAR_0->cd_sector_size - VAR_0->io_buffer_index); } ide_transfer_start(VAR_0, VAR_0->io_buffer + VAR_0->io_buffer_index,
VAR_2, FUNC_0); VAR_0->packet_transfer_size -= VAR_2; VAR_0->elementary_transfer_size -= VAR_2; VAR_0->io_buffer_index += VAR_2; ide_set_irq(VAR_0); #ifdef DEBUG_IDE_ATAPI
printf("status=0x%x\n", VAR_0->status); #endif
} } }