Search
lxdream.org :: lxdream/src/maple/maple.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/maple.c
changeset 838:9abb2fa58934
prev770:429ff505c450
next850:28782ebbd01d
author nkeynes
date Tue Sep 02 00:42:43 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add flag to maple devices indicating whether they need a mouse grab to operate
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@31
     3
 *
nkeynes@31
     4
 * Implements the core Maple bus, including DMA transfers to and from the bus.
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@31
    17
 */
nkeynes@35
    18
#define MODULE maple_module
nkeynes@31
    19
nkeynes@1
    20
#include <assert.h>
nkeynes@480
    21
#include <glib/gstrfuncs.h>
nkeynes@1
    22
#include "dream.h"
nkeynes@1
    23
#include "mem.h"
nkeynes@1
    24
#include "asic.h"
nkeynes@1
    25
#include "maple.h"
nkeynes@1
    26
nkeynes@15
    27
void maple_init( void );
nkeynes@15
    28
nkeynes@15
    29
struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
nkeynes@736
    30
        NULL, NULL, NULL };
nkeynes@15
    31
nkeynes@608
    32
struct maple_device_class *maple_device_classes[] = { 
nkeynes@736
    33
        &controller_class, &keyboard_class, &mouse_class, NULL };
nkeynes@144
    34
nkeynes@15
    35
void maple_init( void )
nkeynes@15
    36
{
nkeynes@15
    37
nkeynes@15
    38
}
nkeynes@15
    39
nkeynes@144
    40
maple_device_t maple_new_device( const gchar *name )
nkeynes@144
    41
{
nkeynes@447
    42
    maple_device_class_t clz = maple_get_device_class(name);
nkeynes@447
    43
    if( clz != NULL ) {
nkeynes@736
    44
        return clz->new_device();
nkeynes@447
    45
    } 
nkeynes@447
    46
    return NULL;
nkeynes@447
    47
}
nkeynes@447
    48
nkeynes@480
    49
maple_device_class_t maple_get_device_class( const gchar *name )
nkeynes@447
    50
{
nkeynes@144
    51
    int i;
nkeynes@144
    52
    for( i=0; maple_device_classes[i] != NULL; i++ ) {
nkeynes@736
    53
        if( g_strcasecmp(maple_device_classes[i]->name, name ) == 0 )
nkeynes@736
    54
            return maple_device_classes[i];
nkeynes@144
    55
    }
nkeynes@144
    56
    return NULL;
nkeynes@144
    57
}
nkeynes@144
    58
nkeynes@447
    59
const struct maple_device_class **maple_get_device_classes()
nkeynes@447
    60
{
nkeynes@480
    61
    return (const struct maple_device_class **)maple_device_classes;
nkeynes@447
    62
}
nkeynes@447
    63
nkeynes@450
    64
lxdream_config_entry_t maple_get_device_config( maple_device_t dev )
nkeynes@144
    65
{
nkeynes@144
    66
    if( dev->get_config == NULL )
nkeynes@736
    67
        return NULL;
nkeynes@144
    68
    return dev->get_config(dev);
nkeynes@144
    69
}
nkeynes@144
    70
nkeynes@2
    71
/**
nkeynes@2
    72
 * Input data looks like this:
nkeynes@2
    73
 *    0: transfer control word
nkeynes@2
    74
 *      0: length of data in words (not including 3 word header)
nkeynes@2
    75
 *      1: low bit = lightgun mode
nkeynes@2
    76
 *      2: low 2 bits = port # (0..3)
nkeynes@2
    77
 *      3: 0x80 = last packet, 0x00 = normal packet
nkeynes@2
    78
 *    4: output buffer address
nkeynes@2
    79
 *    8: Command word
nkeynes@2
    80
 *      8: command code
nkeynes@2
    81
 *      9: destination address
nkeynes@2
    82
 *     10: source address
nkeynes@2
    83
 *     11: length of data in words (not including 3 word header)
nkeynes@2
    84
 *   12: command-specific data
nkeynes@2
    85
 */
nkeynes@2
    86
nkeynes@2
    87
/**
nkeynes@2
    88
 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
nkeynes@2
    89
 * [1][2] is the second subperipheral on port B and so on.
nkeynes@2
    90
 */
nkeynes@2
    91
maple_device_t maple_devices[4][6];
nkeynes@2
    92
int maple_periph_mask[4];
nkeynes@2
    93
#define GETBYTE(n) ((uint32_t)(buf[n]))
nkeynes@2
    94
#define GETWORD(n) (*((uint32_t *)(buf+(n))))
nkeynes@2
    95
#define PUTBYTE(n,x) (buf[n] = (char)x)
nkeynes@2
    96
#define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
nkeynes@2
    97
nkeynes@144
    98
maple_device_t maple_get_device( unsigned int port, unsigned int periph ) {
nkeynes@144
    99
    if( port >= 4 )
nkeynes@736
   100
        return NULL;
nkeynes@144
   101
    if( periph >= 6 )
nkeynes@736
   102
        return NULL;
nkeynes@144
   103
    return maple_devices[port][periph];
nkeynes@144
   104
}
nkeynes@144
   105
nkeynes@1
   106
void maple_handle_buffer( uint32_t address ) {
nkeynes@2
   107
    unsigned char *buf = (unsigned char *)mem_get_region(address);
nkeynes@1
   108
    if( buf == NULL ) {
nkeynes@1
   109
        ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
nkeynes@1
   110
    } else {
nkeynes@2
   111
        unsigned int last = 0;
nkeynes@2
   112
        int i = 0, count;
nkeynes@2
   113
        for( count=0; !last; count++ ) {
nkeynes@494
   114
            unsigned int port, length, mode, periph, periph_id, out_length;
nkeynes@2
   115
            unsigned int cmd, recv_addr, send_addr;
nkeynes@2
   116
            uint32_t return_addr;
nkeynes@2
   117
            unsigned char *return_buf;
nkeynes@736
   118
nkeynes@2
   119
            last = GETBYTE(3) & 0x80; /* indicates last packet */
nkeynes@2
   120
            port = GETBYTE(2) & 0x03;
nkeynes@494
   121
            mode = GETBYTE(1) & 0x07;
nkeynes@2
   122
            length = GETBYTE(0) & 0xFF;
nkeynes@2
   123
            return_addr = GETWORD(4);
nkeynes@447
   124
nkeynes@736
   125
            if( mode == 0x07 ) {
nkeynes@736
   126
                buf += 4;
nkeynes@736
   127
                address +=4; /* skip? */
nkeynes@736
   128
                continue;
nkeynes@736
   129
            }
nkeynes@447
   130
            if( (return_addr & 0x1C000000) != 0x0C000000 ) {
nkeynes@736
   131
                ERROR( "Bad return address in maple packet: %08X", return_addr );
nkeynes@736
   132
                break;
nkeynes@2
   133
            }
nkeynes@2
   134
            return_buf = mem_get_region(return_addr);
nkeynes@2
   135
            cmd = GETBYTE(8);
nkeynes@2
   136
            recv_addr = GETBYTE(9);
nkeynes@2
   137
            send_addr = GETBYTE(10);
nkeynes@2
   138
            /* Sanity checks */
nkeynes@2
   139
            if( GETBYTE(11) != length ||
nkeynes@736
   140
                    send_addr >> 6 != port ||
nkeynes@736
   141
                    recv_addr >> 6 != port ||
nkeynes@736
   142
                    return_buf == NULL ) {
nkeynes@736
   143
                ERROR( "Received bad packet: %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X",
nkeynes@736
   144
                        buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
nkeynes@736
   145
                        buf[8], buf[9], buf[10], buf[11] );
nkeynes@736
   146
                break;
nkeynes@2
   147
            }
nkeynes@2
   148
            periph = 0;
nkeynes@2
   149
            periph_id = recv_addr & 0x3F;
nkeynes@2
   150
            if( periph_id != 0x20 ) {
nkeynes@2
   151
                for( i=0;i<5;i++ ) {
nkeynes@2
   152
                    if( periph_id == (1<<i) ) {
nkeynes@2
   153
                        periph = i+1;
nkeynes@2
   154
                        break;
nkeynes@2
   155
                    }
nkeynes@2
   156
                }
nkeynes@2
   157
                if( periph == 0 ) { /* Bad setting */
nkeynes@2
   158
                    /* ERROR */
nkeynes@2
   159
                }
nkeynes@2
   160
            }
nkeynes@2
   161
nkeynes@2
   162
            maple_device_t dev = maple_devices[port][periph];
nkeynes@2
   163
            if( dev == NULL ) {
nkeynes@2
   164
                /* no device attached */
nkeynes@2
   165
                *((uint32_t *)return_buf) = -1;
nkeynes@2
   166
            } else {
nkeynes@2
   167
                int status, func, block;
nkeynes@2
   168
                out_length = 0;
nkeynes@2
   169
                switch( cmd ) {
nkeynes@736
   170
                case MAPLE_CMD_INFO:
nkeynes@736
   171
                    status = MAPLE_RESP_INFO;
nkeynes@736
   172
                    memcpy( return_buf+4, dev->ident, 112 );
nkeynes@736
   173
                    out_length = 0x1C;
nkeynes@736
   174
                    break;
nkeynes@736
   175
                case MAPLE_CMD_EXT_INFO:
nkeynes@736
   176
                    status = MAPLE_RESP_EXT_INFO;
nkeynes@736
   177
                    memcpy( return_buf+4, dev->ident, 192 );
nkeynes@736
   178
                    out_length = 0x30;
nkeynes@736
   179
                    break;
nkeynes@736
   180
                case MAPLE_CMD_RESET:
nkeynes@736
   181
                    if( dev->reset == NULL )
nkeynes@736
   182
                        status = MAPLE_RESP_ACK;
nkeynes@736
   183
                    else status = dev->reset(dev);
nkeynes@736
   184
                    break;
nkeynes@736
   185
                case MAPLE_CMD_SHUTDOWN:
nkeynes@736
   186
                    if( dev->shutdown == NULL )
nkeynes@736
   187
                        status = MAPLE_RESP_ACK;
nkeynes@736
   188
                    else status = dev->shutdown(dev);
nkeynes@736
   189
                    break;
nkeynes@736
   190
                case MAPLE_CMD_GET_COND:
nkeynes@736
   191
                    func = GETWORD(12);
nkeynes@736
   192
                    if( dev->get_condition == NULL )
nkeynes@736
   193
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@736
   194
                    else status = dev->get_condition(dev, func,
nkeynes@736
   195
                            return_buf+8,
nkeynes@736
   196
                            &out_length );
nkeynes@736
   197
                    if( status == 0 ) {
nkeynes@736
   198
                        out_length++;
nkeynes@736
   199
                        status = MAPLE_RESP_DATA;
nkeynes@736
   200
                        PUTWORD(4,func);
nkeynes@736
   201
                    }
nkeynes@736
   202
                    break;
nkeynes@736
   203
                case MAPLE_CMD_SET_COND:
nkeynes@736
   204
                    func = GETWORD(12);
nkeynes@736
   205
                    if( dev->set_condition == NULL )
nkeynes@736
   206
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@736
   207
                    else status = dev->set_condition(dev, func,
nkeynes@736
   208
                            buf+16,
nkeynes@736
   209
                            length);
nkeynes@736
   210
                    if( status == 0 )
nkeynes@736
   211
                        status = MAPLE_RESP_ACK;
nkeynes@736
   212
                    break;
nkeynes@736
   213
                case MAPLE_CMD_READ_BLOCK:
nkeynes@736
   214
                    func = GETWORD(12);
nkeynes@736
   215
                    block = GETWORD(16);
nkeynes@736
   216
                    if( dev->read_block == NULL )
nkeynes@736
   217
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@736
   218
                    else status = dev->read_block(dev, func, block,
nkeynes@736
   219
                            return_buf+12,
nkeynes@736
   220
                            &out_length );
nkeynes@736
   221
                    if( status == 0 ) {
nkeynes@736
   222
                        status = MAPLE_RESP_DATA;
nkeynes@736
   223
                        PUTWORD(4,func);
nkeynes@736
   224
                        PUTWORD(8,block);
nkeynes@736
   225
                    }
nkeynes@736
   226
                    break;
nkeynes@736
   227
                case MAPLE_CMD_WRITE_BLOCK:
nkeynes@736
   228
                    func = GETWORD(12);
nkeynes@736
   229
                    block = GETWORD(16);
nkeynes@736
   230
                    if( dev->write_block == NULL )
nkeynes@736
   231
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@736
   232
                    else {
nkeynes@736
   233
                        status = dev->write_block(dev, func, block, 
nkeynes@736
   234
                                buf+20, length);
nkeynes@2
   235
                        if( status == 0 )
nkeynes@2
   236
                            status = MAPLE_RESP_ACK;
nkeynes@736
   237
                    }
nkeynes@736
   238
                    break;
nkeynes@736
   239
                default:
nkeynes@736
   240
                    status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   241
                }
nkeynes@2
   242
                return_buf[0] = status;
nkeynes@2
   243
                return_buf[1] = send_addr;
nkeynes@2
   244
                return_buf[2] = recv_addr;
nkeynes@2
   245
                if( periph == 0 )
nkeynes@2
   246
                    return_buf[2] |= maple_periph_mask[port];
nkeynes@2
   247
                return_buf[3] = out_length;
nkeynes@2
   248
            }
nkeynes@2
   249
            buf += 12 + (length<<2);
nkeynes@736
   250
            address += 12 + (length<<2);
nkeynes@2
   251
        }
nkeynes@2
   252
        asic_event( EVENT_MAPLE_DMA );
nkeynes@1
   253
    }
nkeynes@1
   254
}
nkeynes@2
   255
nkeynes@2
   256
void maple_attach_device( maple_device_t dev, unsigned int port,
nkeynes@2
   257
                          unsigned int periph ) {
nkeynes@2
   258
    assert( port < 4 );
nkeynes@2
   259
    assert( periph < 6 );
nkeynes@2
   260
nkeynes@2
   261
    if( maple_devices[port][periph] != NULL ) {
nkeynes@2
   262
        /* Detach existing peripheral first */
nkeynes@2
   263
        maple_detach_device( port, periph );
nkeynes@2
   264
    }
nkeynes@736
   265
nkeynes@2
   266
    maple_devices[port][periph] = dev;
nkeynes@2
   267
    if( periph != 0 )
nkeynes@2
   268
        maple_periph_mask[port] |= (1<<(periph-1));
nkeynes@2
   269
    else maple_periph_mask[port] |= 0x20;
nkeynes@2
   270
    if( dev->attach != NULL ) {
nkeynes@2
   271
        dev->attach( dev );
nkeynes@2
   272
    }
nkeynes@2
   273
}
nkeynes@2
   274
nkeynes@2
   275
void maple_detach_device( unsigned int port, unsigned int periph ) {
nkeynes@2
   276
    assert( port < 4 );
nkeynes@2
   277
    assert( periph < 6 );
nkeynes@2
   278
nkeynes@2
   279
    maple_device_t dev = maple_devices[port][periph];
nkeynes@2
   280
    if( dev == NULL ) /* already detached */
nkeynes@2
   281
        return;
nkeynes@2
   282
    maple_devices[port][periph] = NULL;
nkeynes@2
   283
    if( dev->detach != NULL ) {
nkeynes@2
   284
        dev->detach(dev);
nkeynes@2
   285
    }
nkeynes@447
   286
    if( dev->destroy != NULL ) {
nkeynes@736
   287
        dev->destroy(dev);
nkeynes@447
   288
    }
nkeynes@2
   289
    if( periph == 0 ) {
nkeynes@2
   290
        /* If we detach the main peripheral, we also have to detach all the
nkeynes@2
   291
         * subperipherals, or the system could get quite confused
nkeynes@2
   292
         */
nkeynes@2
   293
        int i;
nkeynes@2
   294
        maple_periph_mask[port] = 0;
nkeynes@2
   295
        for( i=1; i<6; i++ ) {
nkeynes@2
   296
            maple_detach_device(port,i);
nkeynes@2
   297
        }
nkeynes@2
   298
    } else {
nkeynes@2
   299
        maple_periph_mask[port] &= (~(1<<(periph-1)));
nkeynes@2
   300
    }
nkeynes@736
   301
nkeynes@2
   302
}
nkeynes@144
   303
nkeynes@144
   304
void maple_detach_all() {
nkeynes@144
   305
    int i, j;
nkeynes@144
   306
    for( i=0; i<4; i++ ) {
nkeynes@736
   307
        for( j=0; j<6; j++ ) {
nkeynes@736
   308
            if( maple_devices[i][j] != NULL ) {
nkeynes@736
   309
                maple_device_t dev = maple_devices[i][j];
nkeynes@736
   310
                if( dev->detach != NULL )
nkeynes@736
   311
                    dev->detach(dev);
nkeynes@736
   312
                if( dev->destroy != NULL )
nkeynes@736
   313
                    dev->destroy(dev);
nkeynes@736
   314
            }
nkeynes@736
   315
        }
nkeynes@736
   316
        maple_periph_mask[i] = 0;
nkeynes@144
   317
    }
nkeynes@144
   318
}
nkeynes@144
   319
nkeynes@838
   320
void maple_reattach_all() 
nkeynes@838
   321
{
nkeynes@144
   322
    int i, j;
nkeynes@144
   323
    for( i=0; i<4; i++ ) {
nkeynes@736
   324
        for( j=0; j<6; j++ ) {
nkeynes@736
   325
            if( maple_devices[i][j] != NULL ) {
nkeynes@736
   326
                maple_device_t dev = maple_devices[i][j];
nkeynes@736
   327
                if( dev->detach != NULL ) 
nkeynes@736
   328
                    dev->detach(dev);
nkeynes@736
   329
                if( dev->attach != NULL )
nkeynes@736
   330
                    dev->attach(dev);
nkeynes@736
   331
            }
nkeynes@736
   332
        }
nkeynes@144
   333
    }
nkeynes@144
   334
}
nkeynes@608
   335
nkeynes@838
   336
gboolean maple_should_grab()
nkeynes@838
   337
{
nkeynes@838
   338
    int mode = MAPLE_GRAB_DONTCARE;
nkeynes@838
   339
    int i,j;
nkeynes@838
   340
    for( i=0; i<4; i++ ) {
nkeynes@838
   341
        for( j=0; j<6; j++ ) {
nkeynes@838
   342
            if( maple_devices[i][j] != NULL ) {
nkeynes@838
   343
                maple_device_t dev = maple_devices[i][j];
nkeynes@838
   344
                if( dev->grab_mode > mode ) {
nkeynes@838
   345
                    mode = dev->grab_mode;
nkeynes@838
   346
                }
nkeynes@838
   347
            }
nkeynes@838
   348
        }
nkeynes@838
   349
    }
nkeynes@838
   350
    return mode == MAPLE_GRAB_YES;
nkeynes@838
   351
}
nkeynes@838
   352
nkeynes@770
   353
void maple_set_device_config_value( maple_device_t dev, unsigned int key, const gchar *value )
nkeynes@770
   354
{
nkeynes@770
   355
    if( dev != NULL && dev->set_config_value != NULL ) {
nkeynes@770
   356
        dev->set_config_value( dev, key, value );
nkeynes@770
   357
    }
nkeynes@770
   358
}
nkeynes@770
   359
nkeynes@608
   360
void maple_default_destroy( maple_device_t mdev )
nkeynes@608
   361
{
nkeynes@608
   362
    free(mdev);
nkeynes@608
   363
}
.