Search
lxdream.org :: lxdream/src/gdrom/ide.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/ide.c
changeset 31:495e480360d7
prev23:1ec3acd0594d
next35:21a4be098304
author nkeynes
date Sun Dec 25 08:24:11 2005 +0000 (15 years ago)
permissions -rw-r--r--
last change Finish adding header blocks to all files
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@31
     2
 * $Id: ide.c,v 1.4 2005-12-25 08:24:11 nkeynes Exp $
nkeynes@2
     3
 *
nkeynes@31
     4
 * IDE interface implementation
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@2
    17
 */
nkeynes@2
    18
#include <stdlib.h>
nkeynes@15
    19
#include "modules.h"
nkeynes@2
    20
#include "ide.h"
nkeynes@2
    21
nkeynes@2
    22
#define MAX_WRITE_BUF 4096;
nkeynes@2
    23
nkeynes@15
    24
void ide_init( void );
nkeynes@15
    25
void ide_init( void );
nkeynes@15
    26
nkeynes@15
    27
struct dreamcast_module ide_module = { "IDE", ide_init, ide_reset, NULL, NULL,
nkeynes@23
    28
				       NULL, NULL, NULL };
nkeynes@15
    29
nkeynes@2
    30
struct ide_registers idereg;
nkeynes@2
    31
nkeynes@2
    32
static char command_buffer[12];
nkeynes@2
    33
nkeynes@2
    34
/* "\0\0\0\0\xb4\x19\0\0\x08SE      REV 6.42990316" */
nkeynes@2
    35
char gdrom_ident[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x19, 0x00,
nkeynes@2
    36
                       0x00, 0x08, 0x53, 0x45, 0x20, 0x20, 0x20, 0x20,
nkeynes@2
    37
                       0x20, 0x20, 0x52, 0x65, 0x76, 0x20, 0x36, 0x2e,
nkeynes@2
    38
                       0x34, 0x32, 0x39, 0x39, 0x30, 0x33, 0x31, 0x36 };
nkeynes@2
    39
nkeynes@2
    40
nkeynes@2
    41
void set_write_buffer( char *buf, int len )
nkeynes@2
    42
{
nkeynes@2
    43
    idereg.status |= IDE_ST_DATA;
nkeynes@2
    44
    idereg.data = buf;
nkeynes@2
    45
    idereg.datalen = len;
nkeynes@2
    46
    idereg.writeptr = (uint16_t *)buf;
nkeynes@2
    47
    idereg.readptr = NULL;
nkeynes@2
    48
}
nkeynes@2
    49
nkeynes@2
    50
void set_read_buffer( char *buf, int len )
nkeynes@2
    51
{
nkeynes@2
    52
    idereg.status |= IDE_ST_DATA;
nkeynes@2
    53
    idereg.data = buf;
nkeynes@2
    54
    idereg.datalen = len;
nkeynes@2
    55
    idereg.readptr = (uint16_t *)buf;
nkeynes@2
    56
    idereg.writeptr = NULL;
nkeynes@2
    57
    idereg.lba1 = len&0xFF;
nkeynes@2
    58
    idereg.lba2 = len>>8;
nkeynes@2
    59
}
nkeynes@2
    60
nkeynes@2
    61
void ide_clear_interrupt( void )
nkeynes@2
    62
{
nkeynes@2
    63
    /* TODO */
nkeynes@2
    64
}
nkeynes@2
    65
nkeynes@15
    66
void ide_init( void )
nkeynes@15
    67
{
nkeynes@15
    68
nkeynes@15
    69
}
nkeynes@15
    70
nkeynes@2
    71
void ide_reset( void )
nkeynes@2
    72
{
nkeynes@2
    73
    ide_clear_interrupt();
nkeynes@2
    74
    idereg.error = 0x01;
nkeynes@2
    75
    idereg.count = 0x01;
nkeynes@2
    76
    idereg.lba0 = 0x21;
nkeynes@2
    77
    idereg.lba1 = 0x14;
nkeynes@2
    78
    idereg.lba2 = 0xeb;
nkeynes@2
    79
    idereg.feature = 0; /* Indeterminate really */
nkeynes@2
    80
    idereg.status = 0x00;
nkeynes@2
    81
    idereg.device = 0x00;
nkeynes@2
    82
    idereg.disc = IDE_DISC_GDROM | IDE_DISC_READY;
nkeynes@2
    83
}
nkeynes@2
    84
nkeynes@2
    85
uint16_t ide_read_data_pio( void ) {
nkeynes@2
    86
    if( idereg.readptr == NULL )
nkeynes@2
    87
        return 0xFFFF;
nkeynes@2
    88
    uint16_t rv = *idereg.readptr++;
nkeynes@2
    89
    idereg.datalen-=2;
nkeynes@2
    90
    if( idereg.datalen <=0 ) {
nkeynes@2
    91
        idereg.readptr = NULL;
nkeynes@2
    92
        idereg.status &= ~IDE_ST_DATA;
nkeynes@2
    93
    }
nkeynes@2
    94
    return rv;
nkeynes@2
    95
}
nkeynes@2
    96
nkeynes@2
    97
void ide_write_data_pio( uint16_t val ) {
nkeynes@2
    98
    if( idereg.writeptr == NULL )
nkeynes@2
    99
        return;
nkeynes@2
   100
    *idereg.writeptr++ = val;
nkeynes@2
   101
    idereg.datalen-=2;
nkeynes@2
   102
    if( idereg.datalen <= 0 ) {
nkeynes@2
   103
        idereg.writeptr = NULL;
nkeynes@2
   104
        idereg.status &= ~IDE_ST_DATA;
nkeynes@2
   105
        ide_write_buffer( idereg.data );
nkeynes@2
   106
    }
nkeynes@2
   107
}
nkeynes@2
   108
nkeynes@2
   109
void ide_write_control( uint8_t val ) {
nkeynes@2
   110
    /* TODO: In theory we can cause a soft-reset here, but the DC doesn't
nkeynes@2
   111
     * appear to support it.
nkeynes@2
   112
     */
nkeynes@2
   113
}
nkeynes@2
   114
nkeynes@2
   115
void ide_write_command( uint8_t val ) {
nkeynes@2
   116
    idereg.command = val;
nkeynes@2
   117
    switch( val ) {
nkeynes@2
   118
        case IDE_CMD_RESET_DEVICE:
nkeynes@2
   119
            ide_reset();
nkeynes@2
   120
            break;
nkeynes@2
   121
        case IDE_CMD_PACKET:
nkeynes@2
   122
            set_write_buffer(command_buffer,12);
nkeynes@2
   123
            break;
nkeynes@2
   124
        default:
nkeynes@2
   125
            WARN( "IDE: Unimplemented command: %02X", val );
nkeynes@2
   126
    }
nkeynes@2
   127
    idereg.status |= IDE_ST_READY | IDE_ST_SERV;
nkeynes@2
   128
}
nkeynes@2
   129
nkeynes@2
   130
void ide_write_buffer( char *data ) {
nkeynes@2
   131
    uint16_t length;
nkeynes@2
   132
    switch( idereg.command ) {
nkeynes@2
   133
        case IDE_CMD_PACKET:
nkeynes@2
   134
            /* Okay we have the packet in the command buffer */
nkeynes@2
   135
            WARN( "ATAPI: Received Packet command: %02X", data[0] );
nkeynes@2
   136
            
nkeynes@2
   137
            switch( command_buffer[0] ) {
nkeynes@2
   138
                case PKT_CMD_IDENTIFY:
nkeynes@2
   139
                    /* NB: Bios sets data[4] = 0x08, no idea what this is for;
nkeynes@2
   140
                     * different values here appear to have no effect.
nkeynes@2
   141
                     */
nkeynes@2
   142
                    length = *((uint16_t*)(data+2));
nkeynes@2
   143
                    if( length > sizeof(gdrom_ident) )
nkeynes@2
   144
                        length = sizeof(gdrom_ident);
nkeynes@2
   145
                    set_read_buffer(gdrom_ident, length);
nkeynes@2
   146
                    break;
nkeynes@2
   147
            }
nkeynes@2
   148
            break;
nkeynes@2
   149
    }
nkeynes@2
   150
}
.