8 void maple_init( void );
10 struct dreamcast_module maple_module = { "Maple", maple_init, NULL, NULL, NULL,
13 void maple_init( void )
19 * Input data looks like this:
20 * 0: transfer control word
21 * 0: length of data in words (not including 3 word header)
22 * 1: low bit = lightgun mode
23 * 2: low 2 bits = port # (0..3)
24 * 3: 0x80 = last packet, 0x00 = normal packet
25 * 4: output buffer address
28 * 9: destination address
30 * 11: length of data in words (not including 3 word header)
31 * 12: command-specific data
35 * array is [port][subperipheral], so [0][0] is main peripheral on port A,
36 * [1][2] is the second subperipheral on port B and so on.
38 maple_device_t maple_devices[4][6];
39 int maple_periph_mask[4];
40 #define GETBYTE(n) ((uint32_t)(buf[n]))
41 #define GETWORD(n) (*((uint32_t *)(buf+(n))))
42 #define PUTBYTE(n,x) (buf[n] = (char)x)
43 #define PUTWORD(n,x) (*((uint32_t *)(return_buf+(n))) = (x))
45 void maple_handle_buffer( uint32_t address ) {
46 unsigned char *buf = (unsigned char *)mem_get_region(address);
48 ERROR( "Invalid or unmapped buffer passed to maple (0x%08X)", address );
50 unsigned int last = 0;
52 for( count=0; !last; count++ ) {
53 unsigned int port, length, gun, periph, periph_id, out_length;
54 unsigned int cmd, recv_addr, send_addr;
56 unsigned char *return_buf;
58 last = GETBYTE(3) & 0x80; /* indicates last packet */
59 port = GETBYTE(2) & 0x03;
60 gun = GETBYTE(1) & 0x01;
61 length = GETBYTE(0) & 0xFF;
62 return_addr = GETWORD(4);
63 if( return_addr == 0 ) {
66 return_buf = mem_get_region(return_addr);
68 recv_addr = GETBYTE(9);
69 send_addr = GETBYTE(10);
71 if( GETBYTE(11) != length ||
72 send_addr != (port<<6) ||
73 recv_addr >> 6 != port ||
74 return_buf == NULL ) {
78 periph_id = recv_addr & 0x3F;
79 if( periph_id != 0x20 ) {
81 if( periph_id == (1<<i) ) {
86 if( periph == 0 ) { /* Bad setting */
91 INFO( "Maple packet %d: Cmd %d on port %d device %d", count, cmd, port, periph );
92 maple_device_t dev = maple_devices[port][periph];
94 /* no device attached */
95 *((uint32_t *)return_buf) = -1;
97 int status, func, block;
101 status = MAPLE_RESP_INFO;
102 memcpy( return_buf+4, dev->ident, 112 );
105 case MAPLE_CMD_EXT_INFO:
106 status = MAPLE_RESP_EXT_INFO;
107 memcpy( return_buf+4, dev->ident, 192 );
110 case MAPLE_CMD_RESET:
111 if( dev->reset == NULL )
112 status = MAPLE_RESP_ACK;
113 else status = dev->reset(dev);
115 case MAPLE_CMD_SHUTDOWN:
116 if( dev->shutdown == NULL )
117 status = MAPLE_RESP_ACK;
118 else status = dev->shutdown(dev);
120 case MAPLE_CMD_GET_COND:
122 if( dev->get_condition == NULL )
123 status = MAPLE_ERR_CMD_UNKNOWN;
124 else status = dev->get_condition(dev, func,
128 status = MAPLE_RESP_DATA;
132 case MAPLE_CMD_SET_COND:
134 if( dev->set_condition == NULL )
135 status = MAPLE_ERR_CMD_UNKNOWN;
136 else status = dev->set_condition(dev, func,
140 status = MAPLE_RESP_ACK;
142 case MAPLE_CMD_READ_BLOCK:
145 if( dev->read_block == NULL )
146 status = MAPLE_ERR_CMD_UNKNOWN;
147 else status = dev->read_block(dev, func, block,
151 status = MAPLE_RESP_DATA;
156 case MAPLE_CMD_WRITE_BLOCK:
159 if( dev->write_block == NULL )
160 status = MAPLE_ERR_CMD_UNKNOWN;
162 status = dev->write_block(dev, func, block,
165 status = MAPLE_RESP_ACK;
169 status = MAPLE_ERR_CMD_UNKNOWN;
171 return_buf[0] = status;
172 return_buf[1] = send_addr;
173 return_buf[2] = recv_addr;
175 return_buf[2] |= maple_periph_mask[port];
176 return_buf[3] = out_length;
178 buf += 12 + (length<<2);
180 asic_event( EVENT_MAPLE_DMA );
184 void maple_attach_device( maple_device_t dev, unsigned int port,
185 unsigned int periph ) {
187 assert( periph < 6 );
189 if( maple_devices[port][periph] != NULL ) {
190 /* Detach existing peripheral first */
191 maple_detach_device( port, periph );
194 maple_devices[port][periph] = dev;
196 maple_periph_mask[port] |= (1<<(periph-1));
197 else maple_periph_mask[port] |= 0x20;
198 if( dev->attach != NULL ) {
203 void maple_detach_device( unsigned int port, unsigned int periph ) {
205 assert( periph < 6 );
207 maple_device_t dev = maple_devices[port][periph];
208 if( dev == NULL ) /* already detached */
210 maple_devices[port][periph] = NULL;
211 if( dev->detach != NULL ) {
215 /* If we detach the main peripheral, we also have to detach all the
216 * subperipherals, or the system could get quite confused
219 maple_periph_mask[port] = 0;
220 for( i=1; i<6; i++ ) {
221 maple_detach_device(port,i);
224 maple_periph_mask[port] &= (~(1<<(periph-1)));
.