2 * $Id: maple.c,v 1.7 2006-03-20 12:00:15 nkeynes Exp $
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
26 void maple_init( void );
28 struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
31 void maple_init( void )
37 * Input data looks like this:
38 * 0: transfer control word
39 * 0: length of data in words (not including 3 word header)
40 * 1: low bit = lightgun mode
41 * 2: low 2 bits = port # (0..3)
42 * 3: 0x80 = last packet, 0x00 = normal packet
43 * 4: output buffer address
46 * 9: destination address
48 * 11: length of data in words (not including 3 word header)
49 * 12: command-specific data
53 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
54 * [1][2] is the second subperipheral on port B and so on.
56 maple_device_t maple_devices[4][6];
57 int maple_periph_mask[4];
58 #define GETBYTE(n) ((uint32_t)(buf[n]))
59 #define GETWORD(n) (*((uint32_t *)(buf+(n))))
60 #define PUTBYTE(n,x) (buf[n] = (char)x)
61 #define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
63 void maple_handle_buffer( uint32_t address ) {
64 unsigned char *buf = (unsigned char *)mem_get_region(address);
66 ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
68 unsigned int last = 0;
70 for( count=0; !last; count++ ) {
71 unsigned int port, length, gun, periph, periph_id, out_length;
72 unsigned int cmd, recv_addr, send_addr;
74 unsigned char *return_buf;
76 last = GETBYTE(3) & 0x80; /* indicates last packet */
77 port = GETBYTE(2) & 0x03;
78 gun = GETBYTE(1) & 0x01;
79 length = GETBYTE(0) & 0xFF;
80 return_addr = GETWORD(4);
81 if( return_addr == 0 ) {
84 return_buf = mem_get_region(return_addr);
86 recv_addr = GETBYTE(9);
87 send_addr = GETBYTE(10);
89 if( GETBYTE(11) != length ||
90 send_addr != (port<<6) ||
91 recv_addr >> 6 != port ||
92 return_buf == NULL ) {
96 periph_id = recv_addr & 0x3F;
97 if( periph_id != 0x20 ) {
99 if( periph_id == (1<<i) ) {
104 if( periph == 0 ) { /* Bad setting */
109 INFO( "Maple packet %d: Cmd %d on port %d device %d", count, cmd, port, periph );
110 maple_device_t dev = maple_devices[port][periph];
112 /* no device attached */
113 *((uint32_t *)return_buf) = -1;
115 int status, func, block;
119 status = MAPLE_RESP_INFO;
120 memcpy( return_buf+4, dev->ident, 112 );
123 case MAPLE_CMD_EXT_INFO:
124 status = MAPLE_RESP_EXT_INFO;
125 memcpy( return_buf+4, dev->ident, 192 );
128 case MAPLE_CMD_RESET:
129 if( dev->reset == NULL )
130 status = MAPLE_RESP_ACK;
131 else status = dev->reset(dev);
133 case MAPLE_CMD_SHUTDOWN:
134 if( dev->shutdown == NULL )
135 status = MAPLE_RESP_ACK;
136 else status = dev->shutdown(dev);
138 case MAPLE_CMD_GET_COND:
140 if( dev->get_condition == NULL )
141 status = MAPLE_ERR_CMD_UNKNOWN;
142 else status = dev->get_condition(dev, func,
147 status = MAPLE_RESP_DATA;
151 case MAPLE_CMD_SET_COND:
153 if( dev->set_condition == NULL )
154 status = MAPLE_ERR_CMD_UNKNOWN;
155 else status = dev->set_condition(dev, func,
159 status = MAPLE_RESP_ACK;
161 case MAPLE_CMD_READ_BLOCK:
164 if( dev->read_block == NULL )
165 status = MAPLE_ERR_CMD_UNKNOWN;
166 else status = dev->read_block(dev, func, block,
170 status = MAPLE_RESP_DATA;
175 case MAPLE_CMD_WRITE_BLOCK:
178 if( dev->write_block == NULL )
179 status = MAPLE_ERR_CMD_UNKNOWN;
181 status = dev->write_block(dev, func, block,
184 status = MAPLE_RESP_ACK;
188 status = MAPLE_ERR_CMD_UNKNOWN;
190 return_buf[0] = status;
191 return_buf[1] = send_addr;
192 return_buf[2] = recv_addr;
194 return_buf[2] |= maple_periph_mask[port];
195 return_buf[3] = out_length;
197 buf += 12 + (length<<2);
199 asic_event( EVENT_MAPLE_DMA );
203 void maple_attach_device( maple_device_t dev, unsigned int port,
204 unsigned int periph ) {
206 assert( periph < 6 );
208 if( maple_devices[port][periph] != NULL ) {
209 /* Detach existing peripheral first */
210 maple_detach_device( port, periph );
213 maple_devices[port][periph] = dev;
215 maple_periph_mask[port] |= (1<<(periph-1));
216 else maple_periph_mask[port] |= 0x20;
217 if( dev->attach != NULL ) {
222 void maple_detach_device( unsigned int port, unsigned int periph ) {
224 assert( periph < 6 );
226 maple_device_t dev = maple_devices[port][periph];
227 if( dev == NULL ) /* already detached */
229 maple_devices[port][periph] = NULL;
230 if( dev->detach != NULL ) {
234 /* If we detach the main peripheral, we also have to detach all the
235 * subperipherals, or the system could get quite confused
238 maple_periph_mask[port] = 0;
239 for( i=1; i<6; i++ ) {
240 maple_detach_device(port,i);
243 maple_periph_mask[port] &= (~(1<<(periph-1)));
.