1 |
dpavlin |
32 |
/* |
2 |
dpavlin |
34 |
* Copyright (C) 2006-2007 Anders Gavare. All rights reserved. |
3 |
dpavlin |
32 |
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions are met: |
6 |
|
|
* |
7 |
|
|
* 1. Redistributions of source code must retain the above copyright |
8 |
|
|
* notice, this list of conditions and the following disclaimer. |
9 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer in the |
11 |
|
|
* documentation and/or other materials provided with the distribution. |
12 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
13 |
|
|
* derived from this software without specific prior written permission. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
* SUCH DAMAGE. |
26 |
|
|
* |
27 |
|
|
* |
28 |
dpavlin |
34 |
* $Id: dev_dreamcast_maple.c,v 1.11 2007/02/03 20:14:23 debug Exp $ |
29 |
dpavlin |
32 |
* |
30 |
|
|
* Dreamcast "Maple" bus controller. |
31 |
|
|
* |
32 |
|
|
* The Maple bus has 4 ports (A-D), and each port has up to 6 possible "units" |
33 |
|
|
* (where unit 0 is the main unit). Communication is done using DMA; each |
34 |
|
|
* DMA transfer sends one or more requests, and for each of these requests |
35 |
|
|
* a response is generated (or a timeout if there was no device at the |
36 |
|
|
* specific port). |
37 |
|
|
* |
38 |
|
|
* See Marcus Comstedt's page (http://mc.pp.se/dc/maplebus.html) for more |
39 |
|
|
* details about the DMA request/responses. |
40 |
|
|
* |
41 |
|
|
* |
42 |
|
|
* TODO: |
43 |
|
|
* Unit numbers / IDs for real Maple devices. |
44 |
|
|
*/ |
45 |
|
|
|
46 |
|
|
#include <stdio.h> |
47 |
|
|
#include <stdlib.h> |
48 |
|
|
#include <string.h> |
49 |
|
|
#include <sys/time.h> |
50 |
|
|
|
51 |
|
|
#include "console.h" |
52 |
|
|
#include "cpu.h" |
53 |
|
|
#include "device.h" |
54 |
|
|
#include "machine.h" |
55 |
|
|
#include "memory.h" |
56 |
|
|
#include "misc.h" |
57 |
|
|
|
58 |
|
|
#include "dreamcast_maple.h" |
59 |
|
|
#include "dreamcast_sysasicvar.h" |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
#define debug fatal |
63 |
|
|
|
64 |
|
|
#define N_MAPLE_PORTS 4 |
65 |
|
|
|
66 |
|
|
#define MAX_CHARS 8192 |
67 |
|
|
#define MAX_CONTROLLER_DATA 4096 |
68 |
|
|
|
69 |
|
|
#define MAPLE_TICK_SHIFT 17 |
70 |
|
|
|
71 |
|
|
struct maple_device { |
72 |
|
|
struct maple_devinfo devinfo; |
73 |
|
|
}; |
74 |
|
|
|
75 |
|
|
struct dreamcast_maple_data { |
76 |
|
|
/* Registers: */ |
77 |
|
|
uint32_t dmaaddr; |
78 |
|
|
int enable; |
79 |
|
|
int timeout; |
80 |
|
|
|
81 |
|
|
/* Attached devices: */ |
82 |
|
|
struct maple_device *device[N_MAPLE_PORTS]; |
83 |
|
|
|
84 |
|
|
/* For keyboard/controller input: */ |
85 |
|
|
int console_handle; |
86 |
|
|
uint8_t char_queue[MAX_CHARS]; |
87 |
|
|
uint16_t controller_queue[MAX_CONTROLLER_DATA]; |
88 |
|
|
int char_queue_head, char_queue_tail; |
89 |
|
|
int controller_queue_head, controller_queue_tail; |
90 |
|
|
}; |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* |
94 |
|
|
* Maple devices: |
95 |
|
|
* |
96 |
|
|
* TODO: Figure out strings and numbers of _real_ Maple devices. |
97 |
|
|
*/ |
98 |
|
|
static struct maple_device maple_device_controller = { |
99 |
|
|
{ |
100 |
|
|
BE32_TO_HOST(MAPLE_FUNC(MAPLE_FN_CONTROLLER)), /* di_func */ |
101 |
|
|
{ 0,0,0 }, /* di_function_data[3] */ |
102 |
|
|
0, /* di_area_code */ |
103 |
|
|
0, /* di_connector_direction */ |
104 |
|
|
"Dreamcast Controller", /* di_product_name */ |
105 |
|
|
"di_product_license", /* di_product_license */ |
106 |
|
|
LE16_TO_HOST(100), /* di_standby_power */ |
107 |
|
|
LE16_TO_HOST(100) /* di_max_power */ |
108 |
|
|
} |
109 |
|
|
}; |
110 |
|
|
static struct maple_device maple_device_keyboard = { |
111 |
|
|
{ |
112 |
|
|
BE32_TO_HOST(MAPLE_FUNC(MAPLE_FN_KEYBOARD)),/* di_func */ |
113 |
|
|
{ LE32_TO_HOST(2),0,0 }, /* di_function_data[3] */ |
114 |
|
|
0, /* di_area_code */ |
115 |
|
|
0, /* di_connector_direction */ |
116 |
|
|
"Keyboard", /* di_product_name */ |
117 |
|
|
"di_product_license", /* di_product_license */ |
118 |
|
|
LE16_TO_HOST(100), /* di_standby_power */ |
119 |
|
|
LE16_TO_HOST(100) /* di_max_power */ |
120 |
|
|
} |
121 |
|
|
}; |
122 |
|
|
#if 0 |
123 |
|
|
static struct maple_device maple_device_mouse = { |
124 |
|
|
{ |
125 |
|
|
BE32_TO_HOST(MAPLE_FUNC(MAPLE_FN_MOUSE)),/* di_func */ |
126 |
|
|
{ 0,0,0 }, /* di_function_data[3] */ |
127 |
|
|
0, /* di_area_code */ |
128 |
|
|
0, /* di_connector_direction */ |
129 |
|
|
"Dreamcast Mouse", /* di_product_name */ |
130 |
|
|
"di_product_license", /* di_product_license */ |
131 |
|
|
LE16_TO_HOST(100), /* di_standby_power */ |
132 |
|
|
LE16_TO_HOST(100) /* di_max_power */ |
133 |
|
|
} |
134 |
|
|
}; |
135 |
|
|
#endif |
136 |
|
|
|
137 |
|
|
|
138 |
|
|
DEVICE_TICK(maple) |
139 |
|
|
{ |
140 |
|
|
struct dreamcast_maple_data *d = (struct dreamcast_maple_data *) extra; |
141 |
|
|
int control_bits, key = console_readchar(d->console_handle); |
142 |
|
|
|
143 |
|
|
if (key < 0) |
144 |
|
|
return; |
145 |
|
|
|
146 |
|
|
/* Add to the keyboard queue: */ |
147 |
|
|
d->char_queue[d->char_queue_head] = key; |
148 |
|
|
d->char_queue_head = (d->char_queue_head + 1) % MAX_CHARS; |
149 |
|
|
if (d->char_queue_head == d->char_queue_tail) |
150 |
|
|
fatal("[ dreamcast_maple: KEYBOARD QUEUE OVERRUN! ]\n"); |
151 |
|
|
|
152 |
|
|
control_bits = 0; |
153 |
|
|
switch (key) { |
154 |
|
|
case 'a': |
155 |
|
|
case 'A': |
156 |
|
|
control_bits = 0x0004; |
157 |
|
|
break; |
158 |
|
|
case 'b': |
159 |
|
|
case 'B': |
160 |
|
|
control_bits = 0x0002; |
161 |
|
|
break; |
162 |
|
|
case 'c': |
163 |
|
|
case 'C': |
164 |
|
|
control_bits = 0x0001; |
165 |
|
|
break; |
166 |
|
|
case 'x': |
167 |
|
|
case 'X': |
168 |
|
|
control_bits = 0x0400; |
169 |
|
|
break; |
170 |
|
|
case 'y': |
171 |
|
|
case 'Y': |
172 |
|
|
control_bits = 0x0200; |
173 |
|
|
break; |
174 |
|
|
case 'z': |
175 |
|
|
case 'Z': |
176 |
|
|
control_bits = 0x0100; |
177 |
|
|
break; |
178 |
|
|
case 's': |
179 |
|
|
case 'S': /* Start */ |
180 |
|
|
control_bits = 0x0008; |
181 |
|
|
break; |
182 |
|
|
case '8': /* up */ |
183 |
|
|
control_bits = 0x0010; |
184 |
|
|
break; |
185 |
|
|
case '2': /* down */ |
186 |
|
|
case 'k': |
187 |
|
|
control_bits = 0x0020; |
188 |
|
|
break; |
189 |
|
|
case '4': /* left */ |
190 |
|
|
case 'u': |
191 |
|
|
control_bits = 0x0040; |
192 |
|
|
break; |
193 |
|
|
case '6': /* right */ |
194 |
|
|
case 'o': |
195 |
|
|
control_bits = 0x0080; |
196 |
|
|
break; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
if (control_bits != 0) { |
200 |
|
|
/* Add to the controller queue: */ |
201 |
|
|
d->controller_queue[d->controller_queue_head] = control_bits; |
202 |
|
|
d->controller_queue_head = |
203 |
|
|
(d->controller_queue_head + 1) % MAX_CONTROLLER_DATA; |
204 |
|
|
if (d->controller_queue_head == d->controller_queue_tail) |
205 |
|
|
fatal("[ dreamcast_maple: CONTROLLER QUEUE " |
206 |
|
|
"OVERRUN! ]\n"); |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
|
211 |
|
|
static int get_key(struct dreamcast_maple_data *d) |
212 |
|
|
{ |
213 |
|
|
int key = d->char_queue[d->char_queue_tail]; |
214 |
|
|
if (d->char_queue_head == d->char_queue_tail) |
215 |
|
|
return -1; |
216 |
|
|
|
217 |
|
|
d->char_queue_tail = (d->char_queue_tail + 1) % MAX_CHARS; |
218 |
|
|
return key; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
|
222 |
|
|
static int get_controller(struct dreamcast_maple_data *d) |
223 |
|
|
{ |
224 |
|
|
int c = d->controller_queue[d->controller_queue_tail]; |
225 |
|
|
if (d->controller_queue_head == d->controller_queue_tail) |
226 |
|
|
return 0; |
227 |
|
|
|
228 |
|
|
d->controller_queue_tail = (d->controller_queue_tail + 1) |
229 |
|
|
% MAX_CONTROLLER_DATA; |
230 |
|
|
return c; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
/* |
235 |
|
|
* maple_getcond_controller_response(): |
236 |
|
|
* |
237 |
|
|
* Generate a controller response. Based on info from Marcus |
238 |
|
|
* Comstedt's page: http://mc.pp.se/dc/controller.html |
239 |
|
|
*/ |
240 |
|
|
static void maple_getcond_controller_response(struct dreamcast_maple_data *d, |
241 |
|
|
struct cpu *cpu, int port, uint32_t receive_addr) |
242 |
|
|
{ |
243 |
|
|
uint8_t buf[8]; |
244 |
|
|
uint32_t response_code, transfer_code; |
245 |
|
|
int c; |
246 |
|
|
|
247 |
|
|
transfer_code = (MAPLE_RESPONSE_DATATRF << 24) | |
248 |
|
|
(((port << 6) | 0x20) << 16) | |
249 |
|
|
((port << 6) << 8) | |
250 |
|
|
3 /* Transfer length in 32-bit words */; |
251 |
|
|
transfer_code = BE32_TO_HOST(transfer_code); |
252 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &transfer_code, |
253 |
|
|
4, MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
254 |
|
|
receive_addr += 4; |
255 |
|
|
|
256 |
|
|
response_code = BE32_TO_HOST(MAPLE_FUNC(MAPLE_FN_CONTROLLER)); |
257 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &response_code, |
258 |
|
|
4, MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
259 |
|
|
receive_addr += 4; |
260 |
|
|
|
261 |
|
|
/* NOTE: Inverse of the buttons pressed! */ |
262 |
|
|
c = ~get_controller(d); |
263 |
|
|
|
264 |
|
|
/* |
265 |
|
|
* buf[0..1] = little endian button bitfield |
266 |
|
|
* buf[2] = right analogue trigger (0-255) |
267 |
|
|
* buf[3] = left analogue trigger (0-255) |
268 |
|
|
* buf[4] = analogue joystick X (0-255) |
269 |
|
|
* buf[5] = analogue joystick Y (0-255) |
270 |
|
|
* buf[6] = second analogue joystick X (0-255) |
271 |
|
|
* buf[7] = second analogue joystick Y (0-255) |
272 |
|
|
*/ |
273 |
|
|
memset(buf, 0, 8); |
274 |
|
|
buf[0] = c & 0xff; |
275 |
|
|
buf[1] = c >> 8; |
276 |
|
|
buf[4] = buf[5] = buf[6] = buf[7] = 0; |
277 |
|
|
|
278 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &buf, 8, |
279 |
|
|
MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
|
283 |
|
|
/* |
284 |
|
|
* maple_getcond_keyboard_response(): |
285 |
|
|
* |
286 |
|
|
* Generate a keyboard key-press response. Based on info from Marcus |
287 |
|
|
* Comstedt's page: http://mc.pp.se/dc/kbd.html |
288 |
|
|
*/ |
289 |
|
|
static void maple_getcond_keyboard_response(struct dreamcast_maple_data *d, |
290 |
|
|
struct cpu *cpu, int port, uint32_t receive_addr) |
291 |
|
|
{ |
292 |
|
|
int key; |
293 |
|
|
uint8_t buf[8]; |
294 |
|
|
uint32_t response_code, transfer_code; |
295 |
|
|
|
296 |
|
|
transfer_code = (MAPLE_RESPONSE_DATATRF << 24) | |
297 |
|
|
(((port << 6) | 0x20) << 16) | |
298 |
|
|
((port << 6) << 8) | |
299 |
|
|
3 /* Transfer length in 32-bit words */; |
300 |
|
|
transfer_code = BE32_TO_HOST(transfer_code); |
301 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &transfer_code, |
302 |
|
|
4, MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
303 |
|
|
receive_addr += 4; |
304 |
|
|
|
305 |
|
|
response_code = BE32_TO_HOST(MAPLE_FUNC(MAPLE_FN_KEYBOARD)); |
306 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &response_code, |
307 |
|
|
4, MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
308 |
|
|
receive_addr += 4; |
309 |
|
|
|
310 |
|
|
key = get_key(d); |
311 |
|
|
|
312 |
|
|
/* |
313 |
|
|
* buf[0] = shift keys (1 = ctrl, 2 = shift) |
314 |
|
|
* buf[1] = led state |
315 |
|
|
* buf[2] = key |
316 |
|
|
*/ |
317 |
|
|
memset(buf, 0, 8); |
318 |
|
|
|
319 |
|
|
if (key >= 'a' && key <= 'z') buf[2] = 4 + key - 'a'; |
320 |
|
|
if (key >= 'A' && key <= 'Z') buf[0] = 2, buf[2] = 4 + key - 'A'; |
321 |
|
|
if (key >= 1 && key <= 26) buf[0] = 1, buf[2] = 4 + key - 1; |
322 |
|
|
if (key >= '1' && key <= '9') buf[2] = 0x1e + key - '1'; |
323 |
|
|
if (key == '!') buf[0] = 2, buf[2] = 0x1e; |
324 |
|
|
if (key == '"') buf[0] = 2, buf[2] = 0x1f; |
325 |
|
|
if (key == '#') buf[0] = 2, buf[2] = 0x20; |
326 |
|
|
if (key == '$') buf[0] = 2, buf[2] = 0x21; |
327 |
|
|
if (key == '%') buf[0] = 2, buf[2] = 0x22; |
328 |
|
|
if (key == '^') buf[0] = 2, buf[2] = 0x23; |
329 |
|
|
if (key == '&') buf[0] = 2, buf[2] = 0x24; |
330 |
|
|
if (key == '*') buf[0] = 2, buf[2] = 0x25; |
331 |
|
|
if (key == '(') buf[0] = 2, buf[2] = 0x26; |
332 |
|
|
if (key == '@') buf[0] = 2, buf[2] = 0x1f; |
333 |
|
|
if (key == '\n' || key == '\r') buf[0] = 0, buf[2] = 0x28; |
334 |
|
|
if (key == ')') buf[0] = 2, buf[2] = 0x27; |
335 |
|
|
if (key == '\b') buf[0] = 0, buf[2] = 0x2a; |
336 |
|
|
if (key == '\t') buf[0] = 0, buf[2] = 0x2b; |
337 |
|
|
if (key == ' ') buf[0] = 0, buf[2] = 0x2c; |
338 |
|
|
if (key == '0') buf[2] = 0x27; |
339 |
|
|
if (key == 27) buf[2] = 0x29; |
340 |
|
|
if (key == '-') buf[2] = 0x2d; |
341 |
|
|
if (key == '=') buf[2] = 0x2e; |
342 |
|
|
if (key == '[') buf[2] = 0x2f; |
343 |
|
|
if (key == '\\') buf[2] = 0x31; |
344 |
|
|
if (key == '|') buf[2] = 0x31, buf[0] = 2; |
345 |
|
|
if (key == ']') buf[2] = 0x32; |
346 |
|
|
if (key == ';') buf[2] = 0x33; |
347 |
|
|
if (key == ':') buf[2] = 0x34; |
348 |
|
|
if (key == ',') buf[2] = 0x36; |
349 |
|
|
if (key == '.') buf[2] = 0x37; |
350 |
|
|
if (key == '/') buf[2] = 0x38; |
351 |
|
|
if (key == '<') buf[2] = 0x36, buf[0] = 2; |
352 |
|
|
if (key == '>') buf[2] = 0x37, buf[0] = 2; |
353 |
|
|
if (key == '?') buf[2] = 0x38, buf[0] = 2; |
354 |
|
|
if (key == '+') buf[2] = 0x57; |
355 |
|
|
|
356 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, (void *) &buf, 8, |
357 |
|
|
MEM_WRITE, NO_EXCEPTIONS | PHYSICAL); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
|
361 |
|
|
/* |
362 |
|
|
* maple_do_dma_xfer(): |
363 |
|
|
* |
364 |
|
|
* Perform a DMA transfer. enable should be 1, and dmaaddr should point to |
365 |
|
|
* the memory to transfer. |
366 |
|
|
*/ |
367 |
|
|
void maple_do_dma_xfer(struct cpu *cpu, struct dreamcast_maple_data *d) |
368 |
|
|
{ |
369 |
|
|
uint32_t addr = d->dmaaddr; |
370 |
|
|
|
371 |
|
|
if (!d->enable) { |
372 |
|
|
fatal("[ maple_do_dma_xfer: not enabled? ]\n"); |
373 |
|
|
return; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
/* debug("[ dreamcast_maple: DMA transfer, dmaaddr = " |
377 |
|
|
"0x%08"PRIx32" ]\n", addr); */ |
378 |
|
|
|
379 |
|
|
/* |
380 |
|
|
* DMA transfers must be 32-byte aligned, according to Marcus |
381 |
|
|
* Comstedt's Maple demo program. |
382 |
|
|
*/ |
383 |
|
|
if (addr & 0x1f) { |
384 |
|
|
fatal("[ dreamcast_maple: dmaaddr 0x%08"PRIx32" is NOT" |
385 |
|
|
" 32-byte aligned; aborting ]\n", addr); |
386 |
|
|
return; |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
/* |
390 |
|
|
* Handle one or more requests/responses: |
391 |
|
|
* |
392 |
|
|
* (This is "reverse engineered" from Comstedt's maple demo program; |
393 |
|
|
* it might not be good enough to emulate how the Maple is being |
394 |
|
|
* used by other programs.) |
395 |
|
|
*/ |
396 |
|
|
for (;;) { |
397 |
|
|
uint32_t receive_addr, response_code, cond; |
398 |
|
|
int datalen, port, last_message, cmd, to, from, datalen_cmd; |
399 |
|
|
int unit; |
400 |
|
|
uint8_t buf[8]; |
401 |
|
|
|
402 |
|
|
/* Read the message' two control words: */ |
403 |
|
|
cpu->memory_rw(cpu, cpu->mem, addr, (void *) &buf, 8, MEM_READ, |
404 |
|
|
NO_EXCEPTIONS | PHYSICAL); |
405 |
|
|
addr += 8; |
406 |
|
|
|
407 |
|
|
datalen = buf[0] * sizeof(uint32_t); |
408 |
|
|
if (buf[1] & 2) { |
409 |
|
|
fatal("[ dreamcast_maple: TODO: GUN bit. ]\n"); |
410 |
|
|
/* TODO: Set some bits in A05F80C4 to indicate |
411 |
|
|
which raster position a lightgun is pointing |
412 |
|
|
at! */ |
413 |
|
|
exit(1); |
414 |
|
|
} |
415 |
|
|
port = buf[2]; |
416 |
|
|
last_message = buf[3] & 0x80; |
417 |
|
|
receive_addr = buf[4] + (buf[5] << 8) + (buf[6] << 16) |
418 |
|
|
+ (buf[7] << 24); |
419 |
|
|
|
420 |
|
|
if (receive_addr & 0xe000001f) |
421 |
|
|
fatal("[ dreamcast_maple: WARNING! receive address 0x" |
422 |
|
|
"%08"PRIx32" isn't valid! ]\n", receive_addr); |
423 |
|
|
|
424 |
|
|
/* Read the command word for this message: */ |
425 |
|
|
cpu->memory_rw(cpu, cpu->mem, addr, (void *) &buf, 4, MEM_READ, |
426 |
|
|
NO_EXCEPTIONS | PHYSICAL); |
427 |
|
|
addr += 4; |
428 |
|
|
|
429 |
|
|
cmd = buf[0]; |
430 |
|
|
to = buf[1]; |
431 |
|
|
from = buf[2]; |
432 |
|
|
datalen_cmd = buf[3]; |
433 |
|
|
|
434 |
|
|
/* Decode the unit number: */ |
435 |
|
|
unit = 0; |
436 |
|
|
switch (to & 0x3f) { |
437 |
|
|
case 0x00: |
438 |
|
|
case 0x20: unit = 0; break; |
439 |
|
|
case 0x01: unit = 1; break; |
440 |
|
|
case 0x02: unit = 2; break; |
441 |
|
|
case 0x04: unit = 3; break; |
442 |
|
|
case 0x08: unit = 4; break; |
443 |
|
|
case 0x10: unit = 5; break; |
444 |
|
|
default: fatal("[ dreamcast_maple: ERROR! multiple " |
445 |
|
|
"units? Not yet implemented. to = 0x%02x ]\n", to); |
446 |
|
|
exit(1); |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
/* debug("[ dreamcast_maple: cmd=0x%02x, port=%c, unit=%i" |
450 |
|
|
", datalen=%i words ]\n", cmd, port+'A', unit, |
451 |
|
|
datalen_cmd); */ |
452 |
|
|
|
453 |
|
|
/* |
454 |
|
|
* Handle the command: |
455 |
|
|
*/ |
456 |
|
|
switch (cmd) { |
457 |
|
|
|
458 |
|
|
case MAPLE_COMMAND_DEVINFO: |
459 |
|
|
if (d->device[port] == NULL || unit != 0) { |
460 |
|
|
/* No device present: Timeout. */ |
461 |
|
|
/* debug("[ dreamcast_maple: response=" |
462 |
|
|
"timeout ]\n"); */ |
463 |
|
|
response_code = (uint32_t) -1; |
464 |
|
|
response_code = LE32_TO_HOST(response_code); |
465 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, |
466 |
|
|
(void *) &response_code, 4, MEM_WRITE, |
467 |
|
|
NO_EXCEPTIONS | PHYSICAL); |
468 |
|
|
} else { |
469 |
|
|
/* Device present: */ |
470 |
|
|
int i; |
471 |
|
|
struct maple_devinfo *di = |
472 |
|
|
&d->device[port]->devinfo; |
473 |
|
|
/* debug("[ dreamcast_maple: response=" |
474 |
|
|
"\"%s\" ]\n", di->di_product_name); */ |
475 |
|
|
response_code = MAPLE_RESPONSE_DEVINFO | |
476 |
|
|
(((port << 6) | 0x20) << 8) | |
477 |
|
|
((port << 6) << 16) | |
478 |
|
|
((sizeof(struct maple_devinfo) / |
479 |
|
|
sizeof(uint32_t)) << 24); |
480 |
|
|
response_code = LE32_TO_HOST(response_code); |
481 |
|
|
cpu->memory_rw(cpu, cpu->mem, receive_addr, |
482 |
|
|
(void *) &response_code, 4, MEM_WRITE, |
483 |
|
|
NO_EXCEPTIONS | PHYSICAL); |
484 |
|
|
for (i=0; i<sizeof(struct maple_devinfo); i++) |
485 |
|
|
cpu->memory_rw(cpu, cpu->mem, |
486 |
|
|
receive_addr + 4 + i, (unsigned |
487 |
|
|
char *) di + i, 1, MEM_WRITE, |
488 |
|
|
NO_EXCEPTIONS | PHYSICAL); |
489 |
|
|
} |
490 |
|
|
break; |
491 |
|
|
|
492 |
|
|
case MAPLE_COMMAND_GETCOND: |
493 |
|
|
cpu->memory_rw(cpu, cpu->mem, addr, (void *) &buf, 4, |
494 |
|
|
MEM_READ, NO_EXCEPTIONS | PHYSICAL); |
495 |
|
|
cond = buf[3] + (buf[2] << 8) + (buf[1] << 16) |
496 |
|
|
+ (buf[0] << 24); |
497 |
|
|
if (cond & MAPLE_FUNC(MAPLE_FN_CONTROLLER)) { |
498 |
|
|
maple_getcond_controller_response( |
499 |
|
|
d, cpu, port, receive_addr); |
500 |
|
|
} else if (cond & MAPLE_FUNC(MAPLE_FN_KEYBOARD)) { |
501 |
|
|
maple_getcond_keyboard_response( |
502 |
|
|
d, cpu, port, receive_addr); |
503 |
|
|
} else { |
504 |
|
|
fatal("[ dreamcast_maple: WARNING: GETCOND: " |
505 |
|
|
"UNIMPLEMENTED 0x%08"PRIx32" ]\n", cond); |
506 |
|
|
} |
507 |
|
|
break; |
508 |
|
|
|
509 |
|
|
case MAPLE_COMMAND_BWRITE: |
510 |
|
|
fatal("[ dreamcast_maple: BWRITE: TODO ]\n"); |
511 |
|
|
break; |
512 |
|
|
|
513 |
|
|
default:fatal("[ dreamcast_maple: command %i: TODO ]\n", cmd); |
514 |
|
|
exit(1); |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
addr += datalen_cmd * 4; |
518 |
|
|
|
519 |
|
|
/* Last request? Then stop. */ |
520 |
|
|
if (last_message) |
521 |
|
|
break; |
522 |
|
|
} |
523 |
|
|
|
524 |
|
|
/* Assert the SYSASIC_EVENT_MAPLE_DMADONE event: */ |
525 |
|
|
SYSASIC_TRIGGER_EVENT(SYSASIC_EVENT_MAPLE_DMADONE); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
|
529 |
|
|
DEVICE_ACCESS(dreamcast_maple) |
530 |
|
|
{ |
531 |
|
|
struct dreamcast_maple_data *d = (struct dreamcast_maple_data *) extra; |
532 |
|
|
uint64_t idata = 0, odata = 0; |
533 |
|
|
|
534 |
|
|
if (writeflag == MEM_WRITE) |
535 |
|
|
idata = memory_readmax64(cpu, data, len); |
536 |
|
|
|
537 |
|
|
switch (relative_addr) { |
538 |
|
|
|
539 |
|
|
case 0x04: /* MAPLE_DMAADDR */ |
540 |
|
|
if (writeflag == MEM_WRITE) { |
541 |
|
|
d->dmaaddr = idata; |
542 |
|
|
/* debug("[ dreamcast_maple: dmaaddr set to 0x%08x" |
543 |
|
|
" ]\n", d->dmaaddr); */ |
544 |
|
|
} else { |
545 |
|
|
fatal("[ dreamcast_maple: TODO: read from dmaaddr ]\n"); |
546 |
|
|
odata = d->dmaaddr; |
547 |
|
|
} |
548 |
|
|
break; |
549 |
|
|
|
550 |
|
|
case 0x10: /* MAPLE_RESET2 */ |
551 |
|
|
if (writeflag == MEM_WRITE && idata != 0) |
552 |
|
|
fatal("[ dreamcast_maple: UNIMPLEMENTED reset2 value" |
553 |
|
|
" 0x%08x ]\n", (int)idata); |
554 |
|
|
break; |
555 |
|
|
|
556 |
|
|
case 0x14: /* MAPLE_ENABLE */ |
557 |
|
|
if (writeflag == MEM_WRITE) |
558 |
|
|
d->enable = idata; |
559 |
|
|
else |
560 |
|
|
odata = d->enable; |
561 |
|
|
break; |
562 |
|
|
|
563 |
|
|
case 0x18: /* MAPLE_STATE */ |
564 |
|
|
if (writeflag == MEM_WRITE) { |
565 |
|
|
switch (idata) { |
566 |
|
|
case 0: break; |
567 |
|
|
case 1: maple_do_dma_xfer(cpu, d); |
568 |
|
|
break; |
569 |
|
|
default:fatal("[ dreamcast_maple: UNIMPLEMENTED " |
570 |
|
|
"state value %i ]\n", (int)idata); |
571 |
|
|
} |
572 |
|
|
} else { |
573 |
|
|
/* Always return 0 to indicate DMA xfer complete. */ |
574 |
|
|
odata = 0; |
575 |
|
|
} |
576 |
|
|
break; |
577 |
|
|
|
578 |
|
|
case 0x80: /* MAPLE_SPEED */ |
579 |
|
|
if (writeflag == MEM_WRITE) { |
580 |
|
|
d->timeout = (idata >> 16) & 0xffff; |
581 |
|
|
/* TODO: Bits 8..9 are "speed", but only the value |
582 |
|
|
0 (indicating 2 Mbit/s) should be used. */ |
583 |
|
|
debug("[ dreamcast_maple: timeout set to %i ]\n", |
584 |
|
|
d->timeout); |
585 |
|
|
} else { |
586 |
|
|
odata = d->timeout << 16; |
587 |
|
|
} |
588 |
|
|
break; |
589 |
|
|
|
590 |
|
|
case 0x8c: /* MAPLE_RESET */ |
591 |
|
|
if (writeflag == MEM_WRITE) { |
592 |
|
|
if (idata != 0x6155404f) |
593 |
|
|
fatal("[ dreamcast_maple: UNIMPLEMENTED reset " |
594 |
|
|
"value 0x%08x ]\n", (int)idata); |
595 |
|
|
d->enable = 0; |
596 |
|
|
} |
597 |
|
|
break; |
598 |
|
|
|
599 |
|
|
default:if (writeflag == MEM_READ) { |
600 |
|
|
fatal("[ dreamcast_maple: UNIMPLEMENTED read from " |
601 |
|
|
"addr 0x%x ]\n", (int)relative_addr); |
602 |
|
|
} else { |
603 |
|
|
fatal("[ dreamcast_maple: UNIMPLEMENTED write to addr " |
604 |
|
|
"0x%x: 0x%x ]\n", (int)relative_addr, (int)idata); |
605 |
|
|
} |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
if (writeflag == MEM_READ) |
609 |
|
|
memory_writemax64(cpu, data, len, odata); |
610 |
|
|
|
611 |
|
|
return 1; |
612 |
|
|
} |
613 |
|
|
|
614 |
|
|
|
615 |
|
|
DEVINIT(dreamcast_maple) |
616 |
|
|
{ |
617 |
|
|
struct machine *machine = devinit->machine; |
618 |
|
|
struct dreamcast_maple_data *d = |
619 |
|
|
malloc(sizeof(struct dreamcast_maple_data)); |
620 |
|
|
if (d == NULL) { |
621 |
|
|
fprintf(stderr, "out of memory\n"); |
622 |
|
|
exit(1); |
623 |
|
|
} |
624 |
|
|
memset(d, 0, sizeof(struct dreamcast_maple_data)); |
625 |
|
|
|
626 |
|
|
memory_device_register(machine->memory, devinit->name, |
627 |
|
|
0x5f6c00, 0x100, dev_dreamcast_maple_access, d, DM_DEFAULT, NULL); |
628 |
|
|
|
629 |
|
|
/* Devices connected to port A..D: */ |
630 |
|
|
d->device[0] = &maple_device_controller; |
631 |
|
|
d->device[1] = NULL; |
632 |
|
|
d->device[2] = &maple_device_keyboard; |
633 |
|
|
d->device[3] = NULL; /* TODO: &maple_device_mouse; */ |
634 |
|
|
|
635 |
|
|
d->console_handle = console_start_slave_inputonly(machine, "maple", 1); |
636 |
|
|
machine->main_console_handle = d->console_handle; |
637 |
|
|
|
638 |
|
|
machine_add_tickfunction(devinit->machine, dev_maple_tick, d, |
639 |
|
|
MAPLE_TICK_SHIFT, 0.0); |
640 |
|
|
|
641 |
|
|
return 1; |
642 |
|
|
} |
643 |
|
|
|