Search
lxdream.org :: lxdream/src/maple/maple.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/maple.c
changeset 144:7f0714e89aaa
prev121:795fec623ce9
next159:406161fea392
author nkeynes
date Mon May 15 08:28:52 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Rename video_driver to display_driver
Add input source to display
Implement configuration file support
Hook controllers up to configuration
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@144
     2
 * $Id: maple.c,v 1.8 2006-05-15 08:28:52 nkeynes Exp $
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@1
    21
#include "dream.h"
nkeynes@1
    22
#include "mem.h"
nkeynes@1
    23
#include "asic.h"
nkeynes@1
    24
#include "maple.h"
nkeynes@1
    25
nkeynes@15
    26
void maple_init( void );
nkeynes@15
    27
nkeynes@15
    28
struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
nkeynes@23
    29
					 NULL, NULL, NULL };
nkeynes@15
    30
nkeynes@144
    31
struct maple_device_class *maple_device_classes[] = { &controller_class, NULL };
nkeynes@144
    32
nkeynes@15
    33
void maple_init( void )
nkeynes@15
    34
{
nkeynes@15
    35
nkeynes@15
    36
}
nkeynes@15
    37
nkeynes@144
    38
maple_device_t maple_new_device( const gchar *name )
nkeynes@144
    39
{
nkeynes@144
    40
    int i;
nkeynes@144
    41
    for( i=0; maple_device_classes[i] != NULL; i++ ) {
nkeynes@144
    42
	if( g_strcasecmp(maple_device_classes[i]->name, name ) == 0 )
nkeynes@144
    43
	    return maple_device_classes[i]->new_device();
nkeynes@144
    44
    }
nkeynes@144
    45
    return NULL;
nkeynes@144
    46
}
nkeynes@144
    47
nkeynes@144
    48
dreamcast_config_entry_t maple_get_device_config( maple_device_t dev )
nkeynes@144
    49
{
nkeynes@144
    50
    if( dev->get_config == NULL )
nkeynes@144
    51
	return NULL;
nkeynes@144
    52
    return dev->get_config(dev);
nkeynes@144
    53
}
nkeynes@144
    54
nkeynes@2
    55
/**
nkeynes@2
    56
 * Input data looks like this:
nkeynes@2
    57
 *    0: transfer control word
nkeynes@2
    58
 *      0: length of data in words (not including 3 word header)
nkeynes@2
    59
 *      1: low bit = lightgun mode
nkeynes@2
    60
 *      2: low 2 bits = port # (0..3)
nkeynes@2
    61
 *      3: 0x80 = last packet, 0x00 = normal packet
nkeynes@2
    62
 *    4: output buffer address
nkeynes@2
    63
 *    8: Command word
nkeynes@2
    64
 *      8: command code
nkeynes@2
    65
 *      9: destination address
nkeynes@2
    66
 *     10: source address
nkeynes@2
    67
 *     11: length of data in words (not including 3 word header)
nkeynes@2
    68
 *   12: command-specific data
nkeynes@2
    69
 */
nkeynes@2
    70
nkeynes@2
    71
/**
nkeynes@2
    72
 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
nkeynes@2
    73
 * [1][2] is the second subperipheral on port B and so on.
nkeynes@2
    74
 */
nkeynes@2
    75
maple_device_t maple_devices[4][6];
nkeynes@2
    76
int maple_periph_mask[4];
nkeynes@2
    77
#define GETBYTE(n) ((uint32_t)(buf[n]))
nkeynes@2
    78
#define GETWORD(n) (*((uint32_t *)(buf+(n))))
nkeynes@2
    79
#define PUTBYTE(n,x) (buf[n] = (char)x)
nkeynes@2
    80
#define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
nkeynes@2
    81
nkeynes@144
    82
maple_device_t maple_get_device( unsigned int port, unsigned int periph ) {
nkeynes@144
    83
    if( port >= 4 )
nkeynes@144
    84
	return NULL;
nkeynes@144
    85
    if( periph >= 6 )
nkeynes@144
    86
	return NULL;
nkeynes@144
    87
    return maple_devices[port][periph];
nkeynes@144
    88
}
nkeynes@144
    89
nkeynes@1
    90
void maple_handle_buffer( uint32_t address ) {
nkeynes@2
    91
    unsigned char *buf = (unsigned char *)mem_get_region(address);
nkeynes@1
    92
    if( buf == NULL ) {
nkeynes@1
    93
        ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
nkeynes@1
    94
    } else {
nkeynes@2
    95
        unsigned int last = 0;
nkeynes@2
    96
        int i = 0, count;
nkeynes@2
    97
        for( count=0; !last; count++ ) {
nkeynes@2
    98
            unsigned int port, length, gun, periph, periph_id, out_length;
nkeynes@2
    99
            unsigned int cmd, recv_addr, send_addr;
nkeynes@2
   100
            uint32_t return_addr;
nkeynes@2
   101
            unsigned char *return_buf;
nkeynes@1
   102
            
nkeynes@2
   103
            last = GETBYTE(3) & 0x80; /* indicates last packet */
nkeynes@2
   104
            port = GETBYTE(2) & 0x03;
nkeynes@2
   105
            gun = GETBYTE(1) & 0x01;
nkeynes@2
   106
            length = GETBYTE(0) & 0xFF;
nkeynes@2
   107
            return_addr = GETWORD(4);
nkeynes@2
   108
            if( return_addr == 0 ) {
nkeynes@2
   109
                /* ERROR */
nkeynes@2
   110
            }
nkeynes@2
   111
            return_buf = mem_get_region(return_addr);
nkeynes@2
   112
            cmd = GETBYTE(8);
nkeynes@2
   113
            recv_addr = GETBYTE(9);
nkeynes@2
   114
            send_addr = GETBYTE(10);
nkeynes@2
   115
            /* Sanity checks */
nkeynes@2
   116
            if( GETBYTE(11) != length ||
nkeynes@2
   117
                send_addr != (port<<6) ||
nkeynes@2
   118
                recv_addr >> 6 != port ||
nkeynes@2
   119
                return_buf == NULL ) {
nkeynes@2
   120
                /* ERROR */
nkeynes@2
   121
            }
nkeynes@2
   122
            periph = 0;
nkeynes@2
   123
            periph_id = recv_addr & 0x3F;
nkeynes@2
   124
            if( periph_id != 0x20 ) {
nkeynes@2
   125
                for( i=0;i<5;i++ ) {
nkeynes@2
   126
                    if( periph_id == (1<<i) ) {
nkeynes@2
   127
                        periph = i+1;
nkeynes@2
   128
                        break;
nkeynes@2
   129
                    }
nkeynes@2
   130
                }
nkeynes@2
   131
                if( periph == 0 ) { /* Bad setting */
nkeynes@2
   132
                    /* ERROR */
nkeynes@2
   133
                }
nkeynes@2
   134
            }
nkeynes@2
   135
nkeynes@2
   136
            INFO( "Maple packet %d: Cmd %d on port %d device %d", count, cmd, port, periph );
nkeynes@2
   137
            maple_device_t dev = maple_devices[port][periph];
nkeynes@2
   138
            if( dev == NULL ) {
nkeynes@2
   139
                /* no device attached */
nkeynes@2
   140
                *((uint32_t *)return_buf) = -1;
nkeynes@2
   141
            } else {
nkeynes@2
   142
                int status, func, block;
nkeynes@2
   143
                out_length = 0;
nkeynes@2
   144
                switch( cmd ) {
nkeynes@2
   145
                    case MAPLE_CMD_INFO:
nkeynes@2
   146
                        status = MAPLE_RESP_INFO;
nkeynes@2
   147
                        memcpy( return_buf+4, dev->ident, 112 );
nkeynes@2
   148
                        out_length = 0x1C;
nkeynes@2
   149
                        break;
nkeynes@2
   150
                    case MAPLE_CMD_EXT_INFO:
nkeynes@2
   151
                        status = MAPLE_RESP_EXT_INFO;
nkeynes@2
   152
                        memcpy( return_buf+4, dev->ident, 192 );
nkeynes@2
   153
                        out_length = 0x30;
nkeynes@2
   154
                        break;
nkeynes@2
   155
                    case MAPLE_CMD_RESET:
nkeynes@2
   156
                        if( dev->reset == NULL )
nkeynes@2
   157
                            status = MAPLE_RESP_ACK;
nkeynes@2
   158
                        else status = dev->reset(dev);
nkeynes@2
   159
                        break;
nkeynes@2
   160
                    case MAPLE_CMD_SHUTDOWN:
nkeynes@2
   161
                        if( dev->shutdown == NULL )
nkeynes@2
   162
                            status = MAPLE_RESP_ACK;
nkeynes@2
   163
                        else status = dev->shutdown(dev);
nkeynes@2
   164
                        break;
nkeynes@2
   165
                    case MAPLE_CMD_GET_COND:
nkeynes@2
   166
                        func = GETWORD(12);
nkeynes@2
   167
                        if( dev->get_condition == NULL )
nkeynes@2
   168
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   169
                        else status = dev->get_condition(dev, func,
nkeynes@2
   170
                                                         return_buf+8,
nkeynes@2
   171
                                                         &out_length );
nkeynes@121
   172
			out_length++;
nkeynes@2
   173
                        if( status == 0 ) {
nkeynes@2
   174
                            status = MAPLE_RESP_DATA;
nkeynes@2
   175
                            PUTWORD(4,func);
nkeynes@2
   176
                        }
nkeynes@2
   177
                        break;
nkeynes@2
   178
                    case MAPLE_CMD_SET_COND:
nkeynes@2
   179
                        func = GETWORD(12);
nkeynes@2
   180
                        if( dev->set_condition == NULL )
nkeynes@2
   181
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   182
                        else status = dev->set_condition(dev, func,
nkeynes@2
   183
                                                         buf+16,
nkeynes@2
   184
                                                         length);
nkeynes@2
   185
                        if( status == 0 )
nkeynes@2
   186
                            status = MAPLE_RESP_ACK;
nkeynes@2
   187
                        break;
nkeynes@2
   188
                    case MAPLE_CMD_READ_BLOCK:
nkeynes@2
   189
                        func = GETWORD(12);
nkeynes@2
   190
                        block = GETWORD(16);
nkeynes@2
   191
                        if( dev->read_block == NULL )
nkeynes@2
   192
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   193
                        else status = dev->read_block(dev, func, block,
nkeynes@2
   194
                                                      return_buf+12,
nkeynes@2
   195
                                                      &out_length );
nkeynes@2
   196
                        if( status == 0 ) {
nkeynes@2
   197
                            status = MAPLE_RESP_DATA;
nkeynes@2
   198
                            PUTWORD(4,func);
nkeynes@2
   199
                            PUTWORD(8,block);
nkeynes@2
   200
                        }
nkeynes@2
   201
                        break;
nkeynes@2
   202
                    case MAPLE_CMD_WRITE_BLOCK:
nkeynes@2
   203
                        func = GETWORD(12);
nkeynes@2
   204
                        block = GETWORD(16);
nkeynes@2
   205
                        if( dev->write_block == NULL )
nkeynes@2
   206
                            status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   207
                        else {
nkeynes@2
   208
                            status = dev->write_block(dev, func, block, 
nkeynes@2
   209
                                                      buf+20, length);
nkeynes@2
   210
                            if( status == 0 )
nkeynes@2
   211
                                status = MAPLE_RESP_ACK;
nkeynes@2
   212
                        }
nkeynes@2
   213
                        break;
nkeynes@2
   214
                    default:
nkeynes@2
   215
                        status = MAPLE_ERR_CMD_UNKNOWN;
nkeynes@2
   216
                }
nkeynes@2
   217
                return_buf[0] = status;
nkeynes@2
   218
                return_buf[1] = send_addr;
nkeynes@2
   219
                return_buf[2] = recv_addr;
nkeynes@2
   220
                if( periph == 0 )
nkeynes@2
   221
                    return_buf[2] |= maple_periph_mask[port];
nkeynes@2
   222
                return_buf[3] = out_length;
nkeynes@2
   223
            }
nkeynes@2
   224
            buf += 12 + (length<<2);
nkeynes@2
   225
        }
nkeynes@2
   226
        asic_event( EVENT_MAPLE_DMA );
nkeynes@1
   227
    }
nkeynes@1
   228
}
nkeynes@2
   229
nkeynes@2
   230
void maple_attach_device( maple_device_t dev, unsigned int port,
nkeynes@2
   231
                          unsigned int periph ) {
nkeynes@2
   232
    assert( port < 4 );
nkeynes@2
   233
    assert( periph < 6 );
nkeynes@2
   234
nkeynes@2
   235
    if( maple_devices[port][periph] != NULL ) {
nkeynes@2
   236
        /* Detach existing peripheral first */
nkeynes@2
   237
        maple_detach_device( port, periph );
nkeynes@2
   238
    }
nkeynes@2
   239
    
nkeynes@2
   240
    maple_devices[port][periph] = dev;
nkeynes@2
   241
    if( periph != 0 )
nkeynes@2
   242
        maple_periph_mask[port] |= (1<<(periph-1));
nkeynes@2
   243
    else maple_periph_mask[port] |= 0x20;
nkeynes@2
   244
    if( dev->attach != NULL ) {
nkeynes@2
   245
        dev->attach( dev );
nkeynes@2
   246
    }
nkeynes@2
   247
}
nkeynes@2
   248
nkeynes@2
   249
void maple_detach_device( unsigned int port, unsigned int periph ) {
nkeynes@2
   250
    assert( port < 4 );
nkeynes@2
   251
    assert( periph < 6 );
nkeynes@2
   252
nkeynes@2
   253
    maple_device_t dev = maple_devices[port][periph];
nkeynes@2
   254
    if( dev == NULL ) /* already detached */
nkeynes@2
   255
        return;
nkeynes@2
   256
    maple_devices[port][periph] = NULL;
nkeynes@2
   257
    if( dev->detach != NULL ) {
nkeynes@2
   258
        dev->detach(dev);
nkeynes@2
   259
    }
nkeynes@2
   260
    if( periph == 0 ) {
nkeynes@2
   261
        /* If we detach the main peripheral, we also have to detach all the
nkeynes@2
   262
         * subperipherals, or the system could get quite confused
nkeynes@2
   263
         */
nkeynes@2
   264
        int i;
nkeynes@2
   265
        maple_periph_mask[port] = 0;
nkeynes@2
   266
        for( i=1; i<6; i++ ) {
nkeynes@2
   267
            maple_detach_device(port,i);
nkeynes@2
   268
        }
nkeynes@2
   269
    } else {
nkeynes@2
   270
        maple_periph_mask[port] &= (~(1<<(periph-1)));
nkeynes@2
   271
    }
nkeynes@144
   272
    
nkeynes@2
   273
}
nkeynes@144
   274
nkeynes@144
   275
void maple_detach_all() {
nkeynes@144
   276
    int i, j;
nkeynes@144
   277
    for( i=0; i<4; i++ ) {
nkeynes@144
   278
	for( j=0; j<6; j++ ) {
nkeynes@144
   279
	    if( maple_devices[i][j] != NULL ) {
nkeynes@144
   280
		maple_device_t dev = maple_devices[i][j];
nkeynes@144
   281
		if( dev->detach != NULL )
nkeynes@144
   282
		    dev->detach(dev);
nkeynes@144
   283
		if( dev->destroy != NULL )
nkeynes@144
   284
		    dev->destroy(dev);
nkeynes@144
   285
	    }
nkeynes@144
   286
	}
nkeynes@144
   287
	maple_periph_mask[i] = 0;
nkeynes@144
   288
    }
nkeynes@144
   289
}
nkeynes@144
   290
nkeynes@144
   291
void maple_reattach_all() {
nkeynes@144
   292
    int i, j;
nkeynes@144
   293
    for( i=0; i<4; i++ ) {
nkeynes@144
   294
	for( j=0; j<6; j++ ) {
nkeynes@144
   295
	    if( maple_devices[i][j] != NULL ) {
nkeynes@144
   296
		maple_device_t dev = maple_devices[i][j];
nkeynes@144
   297
		if( dev->attach != NULL )
nkeynes@144
   298
		    dev->attach(dev);
nkeynes@144
   299
	    }
nkeynes@144
   300
	}
nkeynes@144
   301
    }
nkeynes@144
   302
}
.