Search
lxdream.org :: lxdream/src/maple/maple.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/maple.c
changeset 2:42349f6ea216
prev1:eea311cfd33e
next15:5194dd0fdb60
author nkeynes
date Sat Aug 21 06:15:49 2004 +0000 (15 years ago)
permissions -rw-r--r--
last change Commit changes into cvs
file annotate diff log raw
nkeynes@1
     1
#include <assert.h>
nkeynes@1
     2
#include "dream.h"
nkeynes@1
     3
#include "mem.h"
nkeynes@1
     4
#include "asic.h"
nkeynes@1
     5
#include "maple.h"
nkeynes@1
     6
nkeynes@2
     7
/**
nkeynes@2
     8
 * Input data looks like this:
nkeynes@2
     9
 *    0: transfer control word
nkeynes@2
    10
 *      0: length of data in words (not including 3 word header)
nkeynes@2
    11
 *      1: low bit = lightgun mode
nkeynes@2
    12
 *      2: low 2 bits = port # (0..3)
nkeynes@2
    13
 *      3: 0x80 = last packet, 0x00 = normal packet
nkeynes@2
    14
 *    4: output buffer address
nkeynes@2
    15
 *    8: Command word
nkeynes@2
    16
 *      8: command code
nkeynes@2
    17
 *      9: destination address
nkeynes@2
    18
 *     10: source address
nkeynes@2
    19
 *     11: length of data in words (not including 3 word header)
nkeynes@2
    20
 *   12: command-specific data
nkeynes@2
    21
 */
nkeynes@2
    22
nkeynes@2
    23
/**
nkeynes@2
    24
 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
nkeynes@2
    25
 * [1][2] is the second subperipheral on port B and so on.
nkeynes@2
    26
 */
nkeynes@2
    27
maple_device_t maple_devices[4][6];
nkeynes@2
    28
int maple_periph_mask[4];
nkeynes@2
    29
#define GETBYTE(n) ((uint32_t)(buf[n]))
nkeynes@2
    30
#define GETWORD(n) (*((uint32_t *)(buf+(n))))
nkeynes@2
    31
#define PUTBYTE(n,x) (buf[n] = (char)x)
nkeynes@2
    32
#define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
nkeynes@2
    33
nkeynes@1
    34
void maple_handle_buffer( uint32_t address ) {
nkeynes@2
    35
    unsigned char *buf = (unsigned char *)mem_get_region(address);
nkeynes@1
    36
    if( buf == NULL ) {
nkeynes@1
    37
        ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
nkeynes@1
    38
    } else {
nkeynes@2
    39
        unsigned int last = 0;
nkeynes@2
    40
        int i = 0, count;
nkeynes@2
    41
        for( count=0; !last; count++ ) {
nkeynes@2
    42
            unsigned int port, length, gun, periph, periph_id, out_length;
nkeynes@2
    43
            unsigned int cmd, recv_addr, send_addr;
nkeynes@2
    44
            uint32_t return_addr;
nkeynes@2
    45
            unsigned char *return_buf;
nkeynes@1
    46
            
nkeynes@2
    47
            last = GETBYTE(3) & 0x80; /* indicates last packet */
nkeynes@2
    48
            port = GETBYTE(2) & 0x03;
nkeynes@2
    49
            gun = GETBYTE(1) & 0x01;
nkeynes@2
    50
            length = GETBYTE(0) & 0xFF;
nkeynes@2
    51
            return_addr = GETWORD(4);
nkeynes@2
    52
            if( return_addr == 0 ) {
nkeynes@2
    53
                /* ERROR */
nkeynes@2
    54
            }
nkeynes@2
    55
            return_buf = mem_get_region(return_addr);
nkeynes@2
    56
            cmd = GETBYTE(8);
nkeynes@2
    57
            recv_addr = GETBYTE(9);
nkeynes@2
    58
            send_addr = GETBYTE(10);
nkeynes@2
    59
            /* Sanity checks */
nkeynes@2
    60
            if( GETBYTE(11) != length ||
nkeynes@2
    61
                send_addr != (port<<6) ||
nkeynes@2
    62
                recv_addr >> 6 != port ||
nkeynes@2
    63
                return_buf == NULL ) {
nkeynes@2
    64
                /* ERROR */
nkeynes@2
    65
            }
nkeynes@2
    66
            periph = 0;
nkeynes@2
    67
            periph_id = recv_addr & 0x3F;
nkeynes@2
    68
            if( periph_id != 0x20 ) {
nkeynes@2
    69
                for( i=0;i<5;i++ ) {
nkeynes@2
    70
                    if( periph_id == (1<<i) ) {
nkeynes@2
    71
                        periph = i+1;
nkeynes@2
    72
                        break;
nkeynes@2
    73
                    }
nkeynes@2
    74
                }
nkeynes@2
    75
                if( periph == 0 ) { /* Bad setting */
nkeynes@2
    76
                    /* ERROR */
nkeynes@2
    77
                }
nkeynes@2
    78
            }
nkeynes@2
    79
nkeynes@2
    80
            INFO( "Maple packet %d: Cmd %d on port %d device %d", count, cmd, port, periph );
nkeynes@2
    81
            maple_device_t dev = maple_devices[port][periph];
nkeynes@2
    82
            if( dev == NULL ) {
nkeynes@2
    83
                /* no device attached */
nkeynes@2
    84
                *((uint32_t *)return_buf) = -1;
nkeynes@2
    85
            } else {
nkeynes@2
    86
                int status, func, block;
nkeynes@2
    87
                out_length = 0;
nkeynes@2
    88
                switch( cmd ) {
nkeynes@2
    89
                    case MAPLE_CMD_INFO:
nkeynes@2
    90
                        status = MAPLE_RESP_INFO;
nkeynes@2
    91
                        memcpy( return_buf+4, dev->ident, 112 );
nkeynes@2
    92
                        out_length = 0x1C;
nkeynes@2
    93
                        break;
nkeynes@2
    94
                    case MAPLE_CMD_EXT_INFO:
nkeynes@2
    95
                        status = MAPLE_RESP_EXT_INFO;
nkeynes@2
    96
                        memcpy( return_buf+4, dev->ident, 192 );
nkeynes@2
    97
                        out_length = 0x30;
nkeynes@2
    98
                        break;
nkeynes@2
    99
                    case MAPLE_CMD_RESET:
nkeynes@2
   100
                        if( dev->reset == NULL )
nkeynes@2
   101
                            status = MAPLE_RESP_ACK;
nkeynes@2
   102
                        else status = dev->reset(dev);
nkeynes@2
   103
                        break;
nkeynes@2
   104
                    case MAPLE_CMD_SHUTDOWN:
nkeynes@2
   105
                        if( dev->shutdown == NULL )
nkeynes@2
   106
                            status = MAPLE_RESP_ACK;
nkeynes@2
   107
                        else status = dev->shutdown(dev);
nkeynes@2
   108
                        break;
nkeynes@2
   109
                    case MAPLE_CMD_GET_COND:
nkeynes@2
   110
                        func = GETWORD(12);
nkeynes@2
   111
                        if( dev->get_condition == NULL )
nkeynes@2
   112
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   113
                        else status = dev->get_condition(dev, func,
nkeynes@2
   114
                                                         return_buf+8,
nkeynes@2
   115
                                                         &out_length );
nkeynes@2
   116
                        if( status == 0 ) {
nkeynes@2
   117
                            status = MAPLE_RESP_DATA;
nkeynes@2
   118
                            PUTWORD(4,func);
nkeynes@2
   119
                        }
nkeynes@2
   120
                        break;
nkeynes@2
   121
                    case MAPLE_CMD_SET_COND:
nkeynes@2
   122
                        func = GETWORD(12);
nkeynes@2
   123
                        if( dev->set_condition == NULL )
nkeynes@2
   124
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   125
                        else status = dev->set_condition(dev, func,
nkeynes@2
   126
                                                         buf+16,
nkeynes@2
   127
                                                         length);
nkeynes@2
   128
                        if( status == 0 )
nkeynes@2
   129
                            status = MAPLE_RESP_ACK;
nkeynes@2
   130
                        break;
nkeynes@2
   131
                    case MAPLE_CMD_READ_BLOCK:
nkeynes@2
   132
                        func = GETWORD(12);
nkeynes@2
   133
                        block = GETWORD(16);
nkeynes@2
   134
                        if( dev->read_block == NULL )
nkeynes@2
   135
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   136
                        else status = dev->read_block(dev, func, block,
nkeynes@2
   137
                                                      return_buf+12,
nkeynes@2
   138
                                                      &out_length );
nkeynes@2
   139
                        if( status == 0 ) {
nkeynes@2
   140
                            status = MAPLE_RESP_DATA;
nkeynes@2
   141
                            PUTWORD(4,func);
nkeynes@2
   142
                            PUTWORD(8,block);
nkeynes@2
   143
                        }
nkeynes@2
   144
                        break;
nkeynes@2
   145
                    case MAPLE_CMD_WRITE_BLOCK:
nkeynes@2
   146
                        func = GETWORD(12);
nkeynes@2
   147
                        block = GETWORD(16);
nkeynes@2
   148
                        if( dev->write_block == NULL )
nkeynes@2
   149
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   150
                        else {
nkeynes@2
   151
                            status = dev->write_block(dev, func, block, 
nkeynes@2
   152
                                                      buf+20, length);
nkeynes@2
   153
                            if( status == 0 )
nkeynes@2
   154
                                status = MAPLE_RESP_ACK;
nkeynes@2
   155
                        }
nkeynes@2
   156
                        break;
nkeynes@2
   157
                    default:
nkeynes@2
   158
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   159
                }
nkeynes@2
   160
                return_buf[0] = status;
nkeynes@2
   161
                return_buf[1] = send_addr;
nkeynes@2
   162
                return_buf[2] = recv_addr;
nkeynes@2
   163
                if( periph == 0 )
nkeynes@2
   164
                    return_buf[2] |= maple_periph_mask[port];
nkeynes@2
   165
                return_buf[3] = out_length;
nkeynes@2
   166
            }
nkeynes@2
   167
            buf += 12 + (length<<2);
nkeynes@2
   168
        }
nkeynes@2
   169
        asic_event( EVENT_MAPLE_DMA );
nkeynes@1
   170
    }
nkeynes@1
   171
}
nkeynes@2
   172
nkeynes@2
   173
void maple_attach_device( maple_device_t dev, unsigned int port,
nkeynes@2
   174
                          unsigned int periph ) {
nkeynes@2
   175
    assert( port < 4 );
nkeynes@2
   176
    assert( periph < 6 );
nkeynes@2
   177
nkeynes@2
   178
    if( maple_devices[port][periph] != NULL ) {
nkeynes@2
   179
        /* Detach existing peripheral first */
nkeynes@2
   180
        maple_detach_device( port, periph );
nkeynes@2
   181
    }
nkeynes@2
   182
    
nkeynes@2
   183
    maple_devices[port][periph] = dev;
nkeynes@2
   184
    if( periph != 0 )
nkeynes@2
   185
        maple_periph_mask[port] |= (1<<(periph-1));
nkeynes@2
   186
    else maple_periph_mask[port] |= 0x20;
nkeynes@2
   187
    if( dev->attach != NULL ) {
nkeynes@2
   188
        dev->attach( dev );
nkeynes@2
   189
    }
nkeynes@2
   190
}
nkeynes@2
   191
nkeynes@2
   192
void maple_detach_device( unsigned int port, unsigned int periph ) {
nkeynes@2
   193
    assert( port < 4 );
nkeynes@2
   194
    assert( periph < 6 );
nkeynes@2
   195
nkeynes@2
   196
    maple_device_t dev = maple_devices[port][periph];
nkeynes@2
   197
    if( dev == NULL ) /* already detached */
nkeynes@2
   198
        return;
nkeynes@2
   199
    maple_devices[port][periph] = NULL;
nkeynes@2
   200
    if( dev->detach != NULL ) {
nkeynes@2
   201
        dev->detach(dev);
nkeynes@2
   202
    }
nkeynes@2
   203
    if( periph == 0 ) {
nkeynes@2
   204
        /* If we detach the main peripheral, we also have to detach all the
nkeynes@2
   205
         * subperipherals, or the system could get quite confused
nkeynes@2
   206
         */
nkeynes@2
   207
        int i;
nkeynes@2
   208
        maple_periph_mask[port] = 0;
nkeynes@2
   209
        for( i=1; i<6; i++ ) {
nkeynes@2
   210
            maple_detach_device(port,i);
nkeynes@2
   211
        }
nkeynes@2
   212
    } else {
nkeynes@2
   213
        maple_periph_mask[port] &= (~(1<<(periph-1)));
nkeynes@2
   214
    }
nkeynes@2
   215
                                    
nkeynes@2
   216
}
.