/[gxemul]/trunk/src/devices/dev_dreamcast_maple.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/src/devices/dev_dreamcast_maple.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Mon Oct 8 16:22:32 2007 UTC (13 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 18455 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1613 2007/06/15 20:11:26 debug Exp $
20070501	Continuing a little on m88k disassembly (control registers,
		more instructions).
		Adding a dummy mvme88k machine mode.
20070502	Re-adding MIPS load/store alignment exceptions.
20070503	Implementing more of the M88K disassembly code.
20070504	Adding disassembly of some more M88K load/store instructions.
		Implementing some relatively simple M88K instructions (br.n,
		xor[.u] imm, and[.u] imm).
20070505	Implementing M88K three-register and, or, xor, and jmp[.n],
		bsr[.n] including function call trace stuff.
		Applying a patch from Bruce M. Simpson which implements the
		SYSCON_BOARD_CPU_CLOCK_FREQ_ID object of the syscon call in
		the yamon PROM emulation.
20070506	Implementing M88K bb0[.n] and bb1[.n], and skeletons for
		ldcr and stcr (although no control regs are implemented yet).
20070509	Found and fixed the bug which caused Linux for QEMU_MIPS to
		stop working in 0.4.5.1: It was a faulty change to the MIPS
		'sc' and 'scd' instructions I made while going through gcc -W
		warnings on 20070428.
20070510	Updating the Linux/QEMU_MIPS section in guestoses.html to
		use mips-test-0.2.tar.gz instead of 0.1.
		A big thank you to Miod Vallat for sending me M88K manuals.
		Implementing more M88K instructions (addu, subu, div[u], mulu,
		ext[u], clr, set, cmp).
20070511	Fixing bugs in the M88K "and" and "and.u" instructions (found
		by comparing against the manual).
		Implementing more M88K instructions (mask[.u], mak, bcnd (auto-
		generated)) and some more control register details.
		Cleanup: Removing the experimental AVR emulation mode and
		corresponding devices; AVR emulation wasn't really meaningful.
		Implementing autogeneration of most M88K loads/stores. The
		rectangle drawing demo (with -O0) for M88K runs :-)
		Beginning on M88K exception handling.
		More M88K instructions: tb0, tb1, rte, sub, jsr[.n].
		Adding some skeleton MVME PROM ("BUG") emulation.
20070512	Fixing a bug in the M88K cmp instruction.
		Adding the M88K lda (scaled register) instruction.
		Fixing bugs in 64-bit (32-bit pairs) M88K loads/stores.
		Removing the unused tick_hz stuff from the machine struct.
		Implementing the M88K xmem instruction. OpenBSD/mvme88k gets
		far enough to display the Copyright banner :-)
		Implementing subu.co (guess), addu.co, addu.ci, ff0, and ff1.
		Adding a dev_mvme187, for MVME187-specific devices/registers.
		OpenBSD/mvme88k prints more boot messages. :)
20070515	Continuing on MVME187 emulation (adding more devices, beginning
		on the CMMUs, etc).
		Adding the M88K and.c, xor.c, and or.c instructions, and making
		sure that mul, div, etc cause exceptions if executed when SFD1
		is disabled.
20070517	Continuing on M88K and MVME187 emulation in general; moving
		the CMMU registers to the CPU struct, separating dev_pcc2 from
		dev_mvme187, and beginning on memory_m88k.c (BATC and PATC).
		Fixing a bug in 64-bit (32-bit pairs) M88K fast stores.
		Implementing the clock part of dev_mk48txx.
		Implementing the M88K fstcr and xcr instructions.
		Implementing m88k_cpu_tlbdump().
		Beginning on the implementation of a separate address space
		for M88K .usr loads/stores.
20070520	Removing the non-working (skeleton) Sandpoint, SonyNEWS, SHARK
		Dnard, and Zaurus machine modes.
		Experimenting with dyntrans to_be_translated read-ahead. It
		seems to give a very small performance increase for MIPS
		emulation, but a large performance degradation for SuperH. Hm.
20070522	Disabling correct SuperH ITLB emulation; it does not seem to be
		necessary in order to let SH4 guest OSes run, and it slows down
		userspace code.
		Implementing "samepage" branches for SuperH emulation, and some
		other minor speed hacks.
20070525	Continuing on M88K memory-related stuff: exceptions, memory
		transaction register contents, etc.
		Implementing the M88K subu.ci instruction.
		Removing the non-working (skeleton) Iyonix machine mode.
		OpenBSD/mvme88k reaches userland :-), starts executing
		/sbin/init's instructions, and issues a few syscalls, before
		crashing.
20070526	Fixing bugs in dev_mk48txx, so that OpenBSD/mvme88k detects
		the correct time-of-day.
		Implementing a generic IRQ controller for the test machines
		(dev_irqc), similar to a proposed patch from Petr Stepan.
		Experimenting some more with translation read-ahead.
		Adding an "expect" script for automated OpenBSD/landisk
		install regression/performance tests.
20070527	Adding a dummy mmEye (SH3) machine mode skeleton.
		FINALLY found the strange M88K bug I have been hunting: I had
		not emulated the SNIP value for exceptions occurring in
		branch delay slots correctly.
		Implementing correct exceptions for 64-bit M88K loads/stores.
		Address to symbol lookups are now disabled when M88K is
		running in usermode (because usermode addresses don't have
		anything to do with supervisor addresses).
20070531	Removing the mmEye machine mode skeleton.
20070604	Some minor code cleanup.
20070605	Moving src/useremul.c into a subdir (src/useremul/), and
		cleaning up some more legacy constructs.
		Adding -Wstrict-aliasing and -fstrict-aliasing detection to
		the configure script.
20070606	Adding a check for broken GCC on Solaris to the configure
		script. (GCC 3.4.3 on Solaris cannot handle static variables
		which are initialized to 0 or NULL. :-/)
		Removing the old (non-working) ARC emulation modes: NEC RD94,
		R94, R96, and R98, and the last traces of Olivetti M700 and
		Deskstation Tyne.
		Removing the non-working skeleton WDSC device (dev_wdsc).
20070607	Thinking about how to use the host's cc + ld at runtime to
		generate native code. (See experiments/native_cc_ld_test.i
		for an example.)
20070608	Adding a program counter sampling timer, which could be useful
		for native code generation experiments.
		The KN02_CSR_NRMMOD bit in the DECstation 5000/200 (KN02) CSR
		should always be set, to allow a 5000/200 PROM to boot.
20070609	Moving out breakpoint details from the machine struct into
		a helper struct, and removing the limit on max nr of
		breakpoints.
20070610	Moving out tick functions into a helper struct as well (which
		also gets rid of the max limit).
20070612	FINALLY figured out why Debian/DECstation stopped working when
		translation read-ahead was enabled: in src/memory_rw.c, the
		call to invalidate_code_translation was made also if the
		memory access was an instruction load (if the page was mapped
		as writable); it shouldn't be called in that case.
20070613	Implementing some more MIPS32/64 revision 2 instructions: di,
		ei, ext, dext, dextm, dextu, and ins.
20070614	Implementing an instruction combination for the NetBSD/arm
		idle loop (making the host not use any cpu if NetBSD/arm
		inside the emulator is not using any cpu).
		Increasing the nr of ARM VPH entries from 128 to 384.
20070615	Removing the ENABLE_arch stuff from the configure script, so
		that all included architectures are included in both release
		and development builds.
		Moving memory related helper functions from misc.c to memory.c.
		Adding preliminary instructions for netbooting NetBSD/pmppc to
		guestoses.html; it doesn't work yet, there are weird timeouts.
		Beginning a total rewrite of the userland emulation modes
		(removing all emulation modes, beginning from scratch with
		NetBSD/MIPS and FreeBSD/Alpha only).
20070616	After fixing a bug in the DEC21143 NIC (the TDSTAT_OWN bit was
		only cleared for the last segment when transmitting, not all
		segments), NetBSD/pmppc boots with root-on-nfs without the
		timeouts. Updating guestoses.html.
		Removing the skeleton PSP (Playstation Portable) mode.
		Moving X11-related stuff in the machine struct into a helper
		struct.
		Cleanup of out-of-memory checks, to use a new CHECK_ALLOCATION
		macro (which prints a meaningful error message).
		Adding a COMMENT to each machine and device (for automagic
		.index comment generation).
		Doing regression testing for the next release.

==============  RELEASE 0.4.6  ==============


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

  ViewVC Help
Powered by ViewVC 1.1.26