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