filename | src/maple/maple.c |
changeset | 1072:d82e04e6d497 |
prev | 1034:7044e01148f0 |
next | 1172:43eef98ff265 |
author | nkeynes |
date | Tue Jul 21 20:33:21 2009 +1000 (14 years ago) |
permissions | -rw-r--r-- |
last change | Heavy configuration management refactor - Configuration groups now take both an on_change event handler and a default keybinding handler, making most keybinding tasks quite simple - GUI configuration all merged into a unified model, drastically reducing the amount of GUI config code. Bonuses - OSX now has a hotkey preference pane - GTK keybinding editor is much more usable |
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, &lightgun_class, &mouse_class, &vmu_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_group_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;
118 maple_device_t dev;
120 last = GETBYTE(3) & 0x80; /* indicates last packet */
121 port = GETBYTE(2) & 0x03;
122 mode = GETBYTE(1) & 0x07;
123 length = GETBYTE(0) & 0xFF;
124 return_addr = GETWORD(4);
126 switch( mode ) {
127 case 2: /* lightgun */
128 dev = maple_devices[port][0];
129 if( dev != NULL && dev->start_gun != NULL ) {
130 dev->start_gun(dev);
131 return; // Pending
132 } else {
133 asic_event( EVENT_MAPLE_DMA );
134 return;
135 }
136 case 7: /* skip */
137 buf += 4;
138 address +=4;
139 continue;
140 }
141 if( (return_addr & 0x1C000000) != 0x0C000000 ) {
142 ERROR( "Bad return address in maple packet: %08X", return_addr );
143 break;
144 }
145 return_buf = mem_get_region(return_addr);
146 cmd = GETBYTE(8);
147 recv_addr = GETBYTE(9);
148 send_addr = GETBYTE(10);
149 /* Sanity checks */
150 if( GETBYTE(11) != length ||
151 send_addr >> 6 != port ||
152 recv_addr >> 6 != port ||
153 return_buf == NULL ) {
154 ERROR( "Received bad packet: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
155 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
156 buf[8], buf[9], buf[10], buf[11] );
157 break;
158 }
159 periph = 0;
160 periph_id = recv_addr & 0x3F;
161 if( periph_id != 0x20 ) {
162 for( i=0;i<5;i++ ) {
163 if( periph_id == (1<<i) ) {
164 periph = i+1;
165 break;
166 }
167 }
168 if( periph == 0 ) { /* Bad setting */
169 /* ERROR */
170 }
171 }
173 dev = maple_devices[port][periph];
174 if( dev == NULL ) {
175 /* no device attached */
176 *((uint32_t *)return_buf) = -1;
177 } else {
178 int status, func;
179 unsigned int pt, phase, block;
180 out_length = 0;
181 switch( cmd ) {
182 case MAPLE_CMD_INFO:
183 status = MAPLE_RESP_INFO;
184 memcpy( return_buf+4, dev->ident, 112 );
185 out_length = 0x1C;
186 if( periph == 0 ) {
187 /* Identify command on the primary device also sets the
188 * bits in the address in the response according to the
189 * sub-peripherals present.
190 */
191 recv_addr &= 0xE0;
192 for( i=0; i<5; i++ ) {
193 if( maple_devices[port][i+1] != NULL ) {
194 recv_addr |= (1<<i);
195 }
196 }
197 }
198 break;
199 case MAPLE_CMD_EXT_INFO:
200 status = MAPLE_RESP_EXT_INFO;
201 memcpy( return_buf+4, dev->ident, 192 );
202 out_length = 0x30;
203 break;
204 case MAPLE_CMD_RESET:
205 if( dev->reset == NULL )
206 status = MAPLE_RESP_ACK;
207 else status = dev->reset(dev);
208 break;
209 case MAPLE_CMD_SHUTDOWN:
210 if( dev->shutdown == NULL )
211 status = MAPLE_RESP_ACK;
212 else status = dev->shutdown(dev);
213 break;
214 case MAPLE_CMD_GET_COND:
215 func = GETWORD(12);
216 if( dev->get_condition == NULL )
217 status = MAPLE_ERR_CMD_UNKNOWN;
218 else status = dev->get_condition(dev, func,
219 return_buf+8,
220 &out_length );
221 if( status == 0 ) {
222 out_length++;
223 status = MAPLE_RESP_DATA;
224 PUTWORD(4,func);
225 }
226 break;
227 case MAPLE_CMD_SET_COND:
228 func = GETWORD(12);
229 if( dev->set_condition == NULL )
230 status = MAPLE_ERR_CMD_UNKNOWN;
231 else status = dev->set_condition(dev, func,
232 buf+16,
233 length-1);
234 if( status == 0 )
235 status = MAPLE_RESP_ACK;
236 break;
237 case MAPLE_CMD_MEM_INFO:
238 func = GETWORD(12);
239 pt = GETWORD(16);
240 if( dev->get_memory_info == NULL )
241 status = MAPLE_ERR_CMD_UNKNOWN;
242 else status = dev->get_memory_info(dev,func, pt, return_buf+8, &out_length);
243 if( status == 0 ) {
244 status = MAPLE_RESP_DATA;
245 PUTWORD(4,func);
246 }
247 break;
248 case MAPLE_CMD_READ_BLOCK:
249 func = GETWORD(12);
250 pt = GETBYTE(16);
251 phase = GETBYTE(17);
252 block = (GETBYTE(18)<<8) | GETBYTE(19);
253 if( dev->read_block == NULL )
254 status = MAPLE_ERR_CMD_UNKNOWN;
255 else status = dev->read_block(dev, func, pt, block, phase,
256 return_buf+12,
257 &out_length );
258 if( status == 0 ) {
259 status = MAPLE_RESP_DATA;
260 PUTWORD(4,func);
261 PUTWORD(8,block);
262 }
263 break;
264 case MAPLE_CMD_WRITE_BLOCK:
265 func = GETWORD(12);
266 pt = GETBYTE(16);
267 phase = GETBYTE(17);
268 block = (GETBYTE(18)<<8) | GETBYTE(19);
269 if( dev->write_block == NULL )
270 status = MAPLE_ERR_CMD_UNKNOWN;
271 else {
272 status = dev->write_block(dev, func, pt, block, phase,
273 buf+20, length-2);
274 if( status == 0 )
275 status = MAPLE_RESP_ACK;
276 }
277 break;
278 case MAPLE_CMD_SYNC_BLOCK:
279 func = GETWORD(12);
280 pt = GETBYTE(16);
281 phase = GETBYTE(17);
282 block = (GETBYTE(18)<<8) | GETBYTE(19);
283 /* TODO: something? */
284 status = MAPLE_RESP_ACK;
285 break;
286 default:
287 status = MAPLE_ERR_CMD_UNKNOWN;
288 }
289 return_buf[0] = status;
290 return_buf[1] = send_addr;
291 return_buf[2] = recv_addr;
292 if( periph == 0 )
293 return_buf[2] |= maple_periph_mask[port];
294 return_buf[3] = out_length;
295 }
296 buf += 12 + (length<<2);
297 address += 12 + (length<<2);
298 }
299 asic_event( EVENT_MAPLE_DMA );
300 }
301 }
303 void maple_attach_device( maple_device_t dev, unsigned int port,
304 unsigned int periph ) {
305 assert( port < 4 );
306 assert( periph < 6 );
308 if( maple_devices[port][periph] != NULL ) {
309 /* Detach existing peripheral first */
310 maple_detach_device( port, periph );
311 }
313 maple_devices[port][periph] = dev;
314 if( periph != 0 )
315 maple_periph_mask[port] |= (1<<(periph-1));
316 else maple_periph_mask[port] |= 0x20;
317 if( dev->attach != NULL ) {
318 dev->attach( dev );
319 }
320 }
322 void maple_detach_device( unsigned int port, unsigned int periph ) {
323 assert( port < 4 );
324 assert( periph < 6 );
326 maple_device_t dev = maple_devices[port][periph];
327 if( dev == NULL ) /* already detached */
328 return;
329 maple_devices[port][periph] = NULL;
330 if( dev->detach != NULL ) {
331 dev->detach(dev);
332 }
333 if( dev->destroy != NULL ) {
334 dev->destroy(dev);
335 }
336 if( periph == 0 ) {
337 /* If we detach the main peripheral, we also have to detach all the
338 * subperipherals, or the system could get quite confused
339 */
340 int i;
341 maple_periph_mask[port] = 0;
342 for( i=1; i<6; i++ ) {
343 maple_detach_device(port,i);
344 }
345 } else {
346 maple_periph_mask[port] &= (~(1<<(periph-1)));
347 }
349 }
351 void maple_detach_all() {
352 int i, j;
353 for( i=0; i<4; i++ ) {
354 for( j=0; j<6; j++ ) {
355 if( maple_devices[i][j] != NULL ) {
356 maple_device_t dev = maple_devices[i][j];
357 if( dev->detach != NULL )
358 dev->detach(dev);
359 if( dev->destroy != NULL )
360 dev->destroy(dev);
361 }
362 }
363 maple_periph_mask[i] = 0;
364 }
365 }
367 void maple_reattach_all()
368 {
369 int i, j;
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 )
375 dev->detach(dev);
376 if( dev->attach != NULL )
377 dev->attach(dev);
378 }
379 }
380 }
381 }
383 gboolean maple_should_grab()
384 {
385 int mode = MAPLE_GRAB_DONTCARE;
386 int i,j;
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->device_class->flags&MAPLE_GRAB_MASK) > mode ) {
392 mode = dev->device_class->flags & MAPLE_GRAB_MASK;
393 }
394 }
395 }
396 }
397 return mode == MAPLE_GRAB_YES;
398 }
400 void maple_default_destroy( maple_device_t mdev )
401 {
402 free(mdev);
403 }
.