4 * Implements the core Maple bus, including DMA transfers to and from the bus.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
18 #define MODULE maple_module
21 #include <glib/gstrfuncs.h>
28 void maple_init( void );
30 struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
33 struct maple_device_class *maple_device_classes[] = {
34 &controller_class, &keyboard_class, &lightgun_class, &mouse_class, &vmu_class, NULL };
37 * Fire interrupt to notify the completion of DMA transfer
39 static void maple_event_handler( int eventid )
41 MMIO_WRITE( ASIC, MAPLE_STATE, 0 );
42 asic_event( EVENT_MAPLE_DMA );
45 void maple_init( void )
47 register_event_callback( EVENT_MAPLE_DMA, maple_event_handler );
50 maple_device_t maple_new_device( const gchar *name )
52 maple_device_class_t clz = maple_get_device_class(name);
54 return clz->new_device();
59 maple_device_class_t maple_get_device_class( const gchar *name )
62 for( i=0; maple_device_classes[i] != NULL; i++ ) {
63 if( g_strcasecmp(maple_device_classes[i]->name, name ) == 0 )
64 return maple_device_classes[i];
69 const struct maple_device_class **maple_get_device_classes()
71 return (const struct maple_device_class **)maple_device_classes;
74 lxdream_config_group_t maple_get_device_config( maple_device_t dev )
76 if( dev->get_config == NULL )
78 return dev->get_config(dev);
82 * Input data looks like this:
83 * 0: transfer control word
84 * 0: length of data in words (not including 3 word header)
85 * 1: low bit = lightgun mode
86 * 2: low 2 bits = port # (0..3)
87 * 3: 0x80 = last packet, 0x00 = normal packet
88 * 4: output buffer address
91 * 9: destination address
93 * 11: length of data in words (not including 3 word header)
94 * 12: command-specific data
98 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
99 * [1][2] is the second subperipheral on port B and so on.
101 maple_device_t maple_devices[4][6];
102 int maple_periph_mask[4];
103 #define GETBYTE(n) ((uint32_t)(buf[n]))
104 #define GETWORD(n) (*((uint32_t *)(buf+(n))))
105 #define PUTBYTE(n,x) (buf[n] = (char)x)
106 #define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
108 maple_device_t maple_get_device( unsigned int port, unsigned int periph ) {
113 return maple_devices[port][periph];
116 void maple_handle_buffer( uint32_t address ) {
117 unsigned char *buf = (unsigned char *)mem_get_region(address);
119 ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
121 unsigned int last = 0;
123 for( count=0; !last; count++ ) {
124 unsigned int port, length, mode, periph, periph_id, out_length;
125 unsigned int cmd, recv_addr, send_addr;
126 uint32_t return_addr;
127 unsigned char *return_buf;
130 last = GETBYTE(3) & 0x80; /* indicates last packet */
131 port = GETBYTE(2) & 0x03;
132 mode = GETBYTE(1) & 0x07;
133 length = GETBYTE(0) & 0xFF;
134 return_addr = GETWORD(4);
137 case 2: /* lightgun */
138 dev = maple_devices[port][0];
139 if( dev != NULL && dev->start_gun != NULL ) {
143 /* FIXME: Determine how long maple IO really takes to process,
144 * which is probably a function of the number of requests.
145 * For now, just use 0.2ms as a reasonable value.
147 event_schedule( EVENT_MAPLE_DMA, 200000 );
155 if( (return_addr & 0x1C000000) != 0x0C000000 ) {
156 ERROR( "Bad return address in maple packet: %08X", return_addr );
159 return_buf = mem_get_region(return_addr);
161 recv_addr = GETBYTE(9);
162 send_addr = GETBYTE(10);
164 if( GETBYTE(11) != length ||
165 /* send_addr >> 6 != port || */ /* TODO: check that this is ignored? */
166 recv_addr >> 6 != port ||
167 return_buf == NULL ) {
168 ERROR( "Received bad packet: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
169 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
170 buf[8], buf[9], buf[10], buf[11] );
174 periph_id = recv_addr & 0x3F;
175 if( periph_id != 0x20 ) {
177 if( periph_id == (1<<i) ) {
182 if( periph == 0 ) { /* Bad setting */
187 dev = maple_devices[port][periph];
189 /* no device attached */
190 *((uint32_t *)return_buf) = -1;
193 unsigned int pt, phase, block, blkid;
197 status = MAPLE_RESP_INFO;
198 memcpy( return_buf+4, dev->ident, 112 );
201 /* Identify command on the primary device also sets the
202 * bits in the address in the response according to the
203 * sub-peripherals present.
206 for( i=0; i<5; i++ ) {
207 if( maple_devices[port][i+1] != NULL ) {
213 case MAPLE_CMD_EXT_INFO:
214 status = MAPLE_RESP_EXT_INFO;
215 memcpy( return_buf+4, dev->ident, 192 );
218 case MAPLE_CMD_RESET:
219 if( dev->reset == NULL )
220 status = MAPLE_RESP_ACK;
221 else status = dev->reset(dev);
223 case MAPLE_CMD_SHUTDOWN:
224 if( dev->shutdown == NULL )
225 status = MAPLE_RESP_ACK;
226 else status = dev->shutdown(dev);
228 case MAPLE_CMD_GET_COND:
230 if( dev->get_condition == NULL )
231 status = MAPLE_ERR_CMD_UNKNOWN;
232 else status = dev->get_condition(dev, func,
237 status = MAPLE_RESP_DATA;
241 case MAPLE_CMD_SET_COND:
243 if( dev->set_condition == NULL )
244 status = MAPLE_ERR_CMD_UNKNOWN;
245 else status = dev->set_condition(dev, func,
249 status = MAPLE_RESP_ACK;
251 case MAPLE_CMD_MEM_INFO:
254 if( dev->get_memory_info == NULL )
255 status = MAPLE_ERR_CMD_UNKNOWN;
256 else status = dev->get_memory_info(dev,func, pt, return_buf+8, &out_length);
259 status = MAPLE_RESP_DATA;
263 case MAPLE_CMD_READ_BLOCK:
267 block = (GETBYTE(18)<<8) | GETBYTE(19);
269 if( dev->read_block == NULL )
270 status = MAPLE_ERR_CMD_UNKNOWN;
271 else status = dev->read_block(dev, func, pt, block, phase,
275 status = MAPLE_RESP_DATA;
281 case MAPLE_CMD_WRITE_BLOCK:
285 block = (GETBYTE(18)<<8) | GETBYTE(19);
286 if( dev->write_block == NULL )
287 status = MAPLE_ERR_CMD_UNKNOWN;
289 status = dev->write_block(dev, func, pt, block, phase,
292 status = MAPLE_RESP_ACK;
295 case MAPLE_CMD_SYNC_BLOCK:
299 block = (GETBYTE(18)<<8) | GETBYTE(19);
300 /* TODO: something? */
301 status = MAPLE_RESP_ACK;
304 status = MAPLE_ERR_CMD_UNKNOWN;
306 return_buf[0] = status;
307 return_buf[1] = send_addr;
308 return_buf[2] = recv_addr;
310 return_buf[2] |= maple_periph_mask[port];
311 return_buf[3] = out_length;
313 buf += 12 + (length<<2);
314 address += 12 + (length<<2);
316 event_schedule( EVENT_MAPLE_DMA, 200000 );
320 void maple_attach_device( maple_device_t dev, unsigned int port,
321 unsigned int periph ) {
323 assert( periph < 6 );
325 if( maple_devices[port][periph] != NULL ) {
326 /* Detach existing peripheral first */
327 maple_detach_device( port, periph );
330 maple_devices[port][periph] = dev;
332 maple_periph_mask[port] |= (1<<(periph-1));
333 else maple_periph_mask[port] |= 0x20;
334 if( dev->attach != NULL ) {
339 void maple_detach_device( unsigned int port, unsigned int periph ) {
341 assert( periph < 6 );
343 maple_device_t dev = maple_devices[port][periph];
344 if( dev == NULL ) /* already detached */
346 maple_devices[port][periph] = NULL;
347 if( dev->detach != NULL ) {
350 if( dev->destroy != NULL ) {
354 /* If we detach the main peripheral, we also have to detach all the
355 * subperipherals, or the system could get quite confused
358 maple_periph_mask[port] = 0;
359 for( i=1; i<6; i++ ) {
360 maple_detach_device(port,i);
363 maple_periph_mask[port] &= (~(1<<(periph-1)));
368 void maple_detach_all() {
370 for( i=0; i<4; i++ ) {
371 for( j=0; j<6; j++ ) {
372 if( maple_devices[i][j] != NULL ) {
373 maple_device_t dev = maple_devices[i][j];
374 if( dev->detach != NULL )
376 if( dev->destroy != NULL )
380 maple_periph_mask[i] = 0;
384 void maple_reattach_all()
387 for( i=0; i<4; i++ ) {
388 for( j=0; j<6; j++ ) {
389 if( maple_devices[i][j] != NULL ) {
390 maple_device_t dev = maple_devices[i][j];
391 if( dev->detach != NULL )
393 if( dev->attach != NULL )
400 gboolean maple_should_grab()
402 int mode = MAPLE_GRAB_DONTCARE;
404 for( i=0; i<4; i++ ) {
405 for( j=0; j<6; j++ ) {
406 if( maple_devices[i][j] != NULL ) {
407 maple_device_t dev = maple_devices[i][j];
408 if( (dev->device_class->flags&MAPLE_GRAB_MASK) > mode ) {
409 mode = dev->device_class->flags & MAPLE_GRAB_MASK;
414 return mode == MAPLE_GRAB_YES;
417 void maple_default_destroy( maple_device_t mdev )
.