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