Search
lxdream.org :: lxdream/src/maple/maple.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/maple.c
changeset 480:d28c2992f5ee
prev450:207461e79f21
next494:67c133a17400
author nkeynes
date Wed Oct 31 11:53:35 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix miscellaneous warnings
view annotate diff log raw
     1 /**
     2  * $Id: maple.c,v 1.12 2007-10-31 11:53:35 nkeynes Exp $
     3  *
     4  * Implements the core Maple bus, including DMA transfers to and from the bus.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    18 #define MODULE maple_module
    20 #include <assert.h>
    21 #include <glib/gstrfuncs.h>
    22 #include "dream.h"
    23 #include "mem.h"
    24 #include "asic.h"
    25 #include "maple.h"
    27 void maple_init( void );
    29 struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
    30 					 NULL, NULL, NULL };
    32 struct maple_device_class *maple_device_classes[] = { &controller_class, NULL };
    34 void maple_init( void )
    35 {
    37 }
    39 maple_device_t maple_new_device( const gchar *name )
    40 {
    41     maple_device_class_t clz = maple_get_device_class(name);
    42     if( clz != NULL ) {
    43 	return clz->new_device();
    44     } 
    45     return NULL;
    46 }
    48 maple_device_class_t maple_get_device_class( const gchar *name )
    49 {
    50     int i;
    51     for( i=0; maple_device_classes[i] != NULL; i++ ) {
    52 	if( g_strcasecmp(maple_device_classes[i]->name, name ) == 0 )
    53 	    return maple_device_classes[i];
    54     }
    55     return NULL;
    56 }
    58 const struct maple_device_class **maple_get_device_classes()
    59 {
    60     return (const struct maple_device_class **)maple_device_classes;
    61 }
    63 lxdream_config_entry_t maple_get_device_config( maple_device_t dev )
    64 {
    65     if( dev->get_config == NULL )
    66 	return NULL;
    67     return dev->get_config(dev);
    68 }
    70 /**
    71  * Input data looks like this:
    72  *    0: transfer control word
    73  *      0: length of data in words (not including 3 word header)
    74  *      1: low bit = lightgun mode
    75  *      2: low 2 bits = port # (0..3)
    76  *      3: 0x80 = last packet, 0x00 = normal packet
    77  *    4: output buffer address
    78  *    8: Command word
    79  *      8: command code
    80  *      9: destination address
    81  *     10: source address
    82  *     11: length of data in words (not including 3 word header)
    83  *   12: command-specific data
    84  */
    86 /**
    87  * array is [port][subperipheral], so [0][0] is main peripheral on port A,
    88  * [1][2] is the second subperipheral on port B and so on.
    89  */
    90 maple_device_t maple_devices[4][6];
    91 int maple_periph_mask[4];
    92 #define GETBYTE(n) ((uint32_t)(buf[n]))
    93 #define GETWORD(n) (*((uint32_t *)(buf+(n))))
    94 #define PUTBYTE(n,x) (buf[n] = (char)x)
    95 #define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
    97 maple_device_t maple_get_device( unsigned int port, unsigned int periph ) {
    98     if( port >= 4 )
    99 	return NULL;
   100     if( periph >= 6 )
   101 	return NULL;
   102     return maple_devices[port][periph];
   103 }
   105 void maple_handle_buffer( uint32_t address ) {
   106     unsigned char *buf = (unsigned char *)mem_get_region(address);
   107     if( buf == NULL ) {
   108         ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
   109     } else {
   110         unsigned int last = 0;
   111         int i = 0, count;
   112         for( count=0; !last; count++ ) {
   113             unsigned int port, length, gun, periph, periph_id, out_length;
   114             unsigned int cmd, recv_addr, send_addr;
   115             uint32_t return_addr;
   116             unsigned char *return_buf;
   118             last = GETBYTE(3) & 0x80; /* indicates last packet */
   119             port = GETBYTE(2) & 0x03;
   120             gun = GETBYTE(1) & 0x01;
   121             length = GETBYTE(0) & 0xFF;
   122             return_addr = GETWORD(4);
   124             if( (return_addr & 0x1C000000) != 0x0C000000 ) {
   125 		ERROR( "Bad return address in maple packet: %08X", return_addr );
   126 		break;
   127             }
   128             return_buf = mem_get_region(return_addr);
   129             cmd = GETBYTE(8);
   130             recv_addr = GETBYTE(9);
   131             send_addr = GETBYTE(10);
   132             /* Sanity checks */
   133             if( GETBYTE(11) != length ||
   134                 send_addr >> 6 != port ||
   135                 recv_addr >> 6 != port ||
   136                 return_buf == NULL ) {
   137 		ERROR( "Received bad packet: %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X",
   138 		       buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
   139 		       buf[8], buf[9], buf[10], buf[11] );
   140 		break;
   141             }
   142             periph = 0;
   143             periph_id = recv_addr & 0x3F;
   144             if( periph_id != 0x20 ) {
   145                 for( i=0;i<5;i++ ) {
   146                     if( periph_id == (1<<i) ) {
   147                         periph = i+1;
   148                         break;
   149                     }
   150                 }
   151                 if( periph == 0 ) { /* Bad setting */
   152                     /* ERROR */
   153                 }
   154             }
   156             maple_device_t dev = maple_devices[port][periph];
   157             if( dev == NULL ) {
   158                 /* no device attached */
   159                 *((uint32_t *)return_buf) = -1;
   160             } else {
   161                 int status, func, block;
   162                 out_length = 0;
   163                 switch( cmd ) {
   164                     case MAPLE_CMD_INFO:
   165                         status = MAPLE_RESP_INFO;
   166                         memcpy( return_buf+4, dev->ident, 112 );
   167                         out_length = 0x1C;
   168                         break;
   169                     case MAPLE_CMD_EXT_INFO:
   170                         status = MAPLE_RESP_EXT_INFO;
   171                         memcpy( return_buf+4, dev->ident, 192 );
   172                         out_length = 0x30;
   173                         break;
   174                     case MAPLE_CMD_RESET:
   175                         if( dev->reset == NULL )
   176                             status = MAPLE_RESP_ACK;
   177                         else status = dev->reset(dev);
   178                         break;
   179                     case MAPLE_CMD_SHUTDOWN:
   180                         if( dev->shutdown == NULL )
   181                             status = MAPLE_RESP_ACK;
   182                         else status = dev->shutdown(dev);
   183                         break;
   184                     case MAPLE_CMD_GET_COND:
   185                         func = GETWORD(12);
   186                         if( dev->get_condition == NULL )
   187                             status = MAPLE_ERR_CMD_UNKNOWN;
   188                         else status = dev->get_condition(dev, func,
   189                                                          return_buf+8,
   190                                                          &out_length );
   191 			out_length++;
   192                         if( status == 0 ) {
   193                             status = MAPLE_RESP_DATA;
   194                             PUTWORD(4,func);
   195                         }
   196                         break;
   197                     case MAPLE_CMD_SET_COND:
   198                         func = GETWORD(12);
   199                         if( dev->set_condition == NULL )
   200                             status = MAPLE_ERR_CMD_UNKNOWN;
   201                         else status = dev->set_condition(dev, func,
   202                                                          buf+16,
   203                                                          length);
   204                         if( status == 0 )
   205                             status = MAPLE_RESP_ACK;
   206                         break;
   207                     case MAPLE_CMD_READ_BLOCK:
   208                         func = GETWORD(12);
   209                         block = GETWORD(16);
   210                         if( dev->read_block == NULL )
   211                             status = MAPLE_ERR_CMD_UNKNOWN;
   212                         else status = dev->read_block(dev, func, block,
   213                                                       return_buf+12,
   214                                                       &out_length );
   215                         if( status == 0 ) {
   216                             status = MAPLE_RESP_DATA;
   217                             PUTWORD(4,func);
   218                             PUTWORD(8,block);
   219                         }
   220                         break;
   221                     case MAPLE_CMD_WRITE_BLOCK:
   222                         func = GETWORD(12);
   223                         block = GETWORD(16);
   224                         if( dev->write_block == NULL )
   225                             status = MAPLE_ERR_CMD_UNKNOWN;
   226                         else {
   227                             status = dev->write_block(dev, func, block, 
   228                                                       buf+20, length);
   229                             if( status == 0 )
   230                                 status = MAPLE_RESP_ACK;
   231                         }
   232                         break;
   233                     default:
   234                         status = MAPLE_ERR_CMD_UNKNOWN;
   235                 }
   236                 return_buf[0] = status;
   237                 return_buf[1] = send_addr;
   238                 return_buf[2] = recv_addr;
   239                 if( periph == 0 )
   240                     return_buf[2] |= maple_periph_mask[port];
   241                 return_buf[3] = out_length;
   242             }
   243             buf += 12 + (length<<2);
   244 	    address += 12 + (length<<2);
   245         }
   246         asic_event( EVENT_MAPLE_DMA );
   247     }
   248 }
   250 void maple_attach_device( maple_device_t dev, unsigned int port,
   251                           unsigned int periph ) {
   252     assert( port < 4 );
   253     assert( periph < 6 );
   255     if( maple_devices[port][periph] != NULL ) {
   256         /* Detach existing peripheral first */
   257         maple_detach_device( port, periph );
   258     }
   260     maple_devices[port][periph] = dev;
   261     if( periph != 0 )
   262         maple_periph_mask[port] |= (1<<(periph-1));
   263     else maple_periph_mask[port] |= 0x20;
   264     if( dev->attach != NULL ) {
   265         dev->attach( dev );
   266     }
   267 }
   269 void maple_detach_device( unsigned int port, unsigned int periph ) {
   270     assert( port < 4 );
   271     assert( periph < 6 );
   273     maple_device_t dev = maple_devices[port][periph];
   274     if( dev == NULL ) /* already detached */
   275         return;
   276     maple_devices[port][periph] = NULL;
   277     if( dev->detach != NULL ) {
   278         dev->detach(dev);
   279     }
   280     if( dev->destroy != NULL ) {
   281 	dev->destroy(dev);
   282     }
   283     if( periph == 0 ) {
   284         /* If we detach the main peripheral, we also have to detach all the
   285          * subperipherals, or the system could get quite confused
   286          */
   287         int i;
   288         maple_periph_mask[port] = 0;
   289         for( i=1; i<6; i++ ) {
   290             maple_detach_device(port,i);
   291         }
   292     } else {
   293         maple_periph_mask[port] &= (~(1<<(periph-1)));
   294     }
   296 }
   298 void maple_detach_all() {
   299     int i, j;
   300     for( i=0; i<4; i++ ) {
   301 	for( j=0; j<6; j++ ) {
   302 	    if( maple_devices[i][j] != NULL ) {
   303 		maple_device_t dev = maple_devices[i][j];
   304 		if( dev->detach != NULL )
   305 		    dev->detach(dev);
   306 		if( dev->destroy != NULL )
   307 		    dev->destroy(dev);
   308 	    }
   309 	}
   310 	maple_periph_mask[i] = 0;
   311     }
   312 }
   314 void maple_reattach_all() {
   315     int i, j;
   316     for( i=0; i<4; i++ ) {
   317 	for( j=0; j<6; j++ ) {
   318 	    if( maple_devices[i][j] != NULL ) {
   319 		maple_device_t dev = maple_devices[i][j];
   320 		if( dev->attach != NULL )
   321 		    dev->attach(dev);
   322 	    }
   323 	}
   324     }
   325 }
.