/[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 32 - (hide annotations)
Mon Oct 8 16:20:58 2007 UTC (13 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 18366 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1421 2006/11/06 05:32:37 debug Exp $
20060816	Adding a framework for emulated/virtual timers (src/timer.c),
		using only setitimer().
		Rewriting the mc146818 to use the new timer framework.
20060817	Adding a call to gettimeofday() every now and then (once every
		second, at the moment) to resynch the timer if it drifts.
		Beginning to convert the ISA timer interrupt mechanism (8253
		and 8259) to use the new timer framework.
		Removing the -I command line option.
20060819	Adding the -I command line option again, with new semantics.
		Working on Footbridge timer interrupts; NetBSD/NetWinder and
		NetBSD/CATS now run at correct speed, but unfortunately with
		HUGE delays during bootup.
20060821	Some minor m68k updates. Adding the first instruction: nop. :)
		Minor Alpha emulation updates.
20060822	Adding a FreeBSD development specific YAMON environment
		variable ("khz") (as suggested by Bruce M. Simpson).
		Moving YAMON environment variable initialization from
		machine_evbmips.c into promemul/yamon.c, and adding some more
		variables.
		Continuing on the LCA PCI bus controller (for Alpha machines).
20060823	Continuing on the timer stuff: experimenting with MIPS count/
		compare interrupts connected to the timer framework.
20060825	Adding bogus SCSI commands 0x51 (SCSICDROM_READ_DISCINFO) and
		0x52 (SCSICDROM_READ_TRACKINFO) to the SCSI emulation layer,
		to allow NetBSD/pmax 4.0_BETA to be installed from CDROM.
		Minor updates to the LCA PCI controller.
20060827	Implementing a CHIP8 cpu mode, and a corresponding CHIP8
		machine, for fun. Disassembly support for all instructions,
		and most of the common instructions have been implemented: mvi,
		mov_imm, add_imm, jmp, rand, cls, sprite, skeq_imm, jsr,
		skne_imm, bcd, rts, ldr, str, mov, or, and, xor, add, sub,
		font, ssound, sdelay, gdelay, bogus skup/skpr, skeq, skne.
20060828	Beginning to convert the CHIP8 cpu in the CHIP8 machine to a
		(more correct) RCA 180x cpu. (Disassembly for all 1802
		instructions has been implemented, but no execution yet, and
		no 1805 extended instructions.)
20060829	Minor Alpha emulation updates.
20060830	Beginning to experiment a little with PCI IDE for SGI O2.
		Fixing the cursor key mappings for MobilePro 770 emulation.
		Fixing the LK201 warning caused by recent NetBSD/pmax.
		The MIPS R41xx standby, suspend, and hibernate instructions now
		behave like the RM52xx/MIPS32/MIPS64 wait instruction.
		Fixing dev_wdc so it calculates correct (64-bit) offsets before
		giving them to diskimage_access().
20060831	Continuing on Alpha emulation (OSF1 PALcode).
20060901	Minor Alpha updates; beginning on virtual memory pagetables.
		Removed the limit for max nr of devices (in preparation for
		allowing devices' base addresses to be changed during runtime).
		Adding a hack for MIPS [d]mfc0 select 0 (except the count
		register), so that the coproc register is simply copied.
		The MIPS suspend instruction now exits the emulator, instead
		of being treated as a wait instruction (this causes NetBSD/
		hpcmips to get correct 'halt' behavior).
		The VR41xx RTC now returns correct time.
		Connecting the VR41xx timer to the timer framework (fixed at
		128 Hz, for now).
		Continuing on SPARC emulation, adding more instructions:
		restore, ba_xcc, ble. The rectangle drawing demo works :)
		Removing the last traces of the old ENABLE_CACHE_EMULATION
		MIPS stuff (not usable with dyntrans anyway).
20060902	Splitting up src/net.c into several smaller files in its own
		subdirectory (src/net/).
20060903	Cleanup of the files in src/net/, to make them less ugly.
20060904	Continuing on the 'settings' subsystem.
		Minor progress on the SPARC emulation mode.
20060905	Cleanup of various things, and connecting the settings
		infrastructure to various subsystems (emul, machine, cpu, etc).
		Changing the lk201 mouse update routine to not rely on any
		emulated hardware framebuffer cursor coordinates, but instead
		always do (semi-usable) relative movements.
20060906	Continuing on the lk201 mouse stuff. Mouse behaviour with
		multiple framebuffers (which was working in Ultrix) is now
		semi-broken (but it still works, in a way).
		Moving the documentation about networking into its own file
		(networking.html), and refreshing it a bit. Adding an example
		of how to use ethernet frame direct-access (udp_snoop).
20060907	Continuing on the settings infrastructure.
20060908	Minor updates to SH emulation: for 32-bit emulation: delay
		slots and the 'jsr @Rn' instruction. I'm putting 64-bit SH5 on
		ice, for now.
20060909-10	Implementing some more 32-bit SH instructions. Removing the
		64-bit mode completely. Enough has now been implemented to run
		the rectangle drawing demo. :-)
20060912	Adding more SH instructions.
20060916	Continuing on SH emulation (some more instructions: div0u,
		div1, rotcl/rotcr, more mov instructions, dt, braf, sets, sett,
		tst_imm, dmuls.l, subc, ldc_rm_vbr, movt, clrt, clrs, clrmac).
		Continuing on the settings subsystem (beginning on reading/
		writing settings, removing bugs, and connecting more cpus to
		the framework).
20060919	More work on SH emulation; adding an ldc banked instruction,
		and attaching a 640x480 framebuffer to the Dreamcast machine
		mode (NetBSD/dreamcast prints the NetBSD copyright banner :-),
		and then panics).
20060920	Continuing on the settings subsystem.
20060921	Fixing the Footbridge timer stuff so that NetBSD/cats and
		NetBSD/netwinder boot up without the delays.
20060922	Temporarily hardcoding MIPS timer interrupt to 100 Hz. With
		'wait' support disabled, NetBSD/malta and Linux/malta run at
		correct speed.
20060923	Connecting dev_gt to the timer framework, so that NetBSD/cobalt
		runs at correct speed.
		Moving SH4-specific memory mapped registers into its own
		device (dev_sh4.c).
		Running with -N now prints "idling" instead of bogus nr of
		instrs/second (which isn't valid anyway) while idling.
20060924	Algor emulation should now run at correct speed.
		Adding disassembly support for some MIPS64 revision 2
		instructions: ext, dext, dextm, dextu.
20060926	The timer framework now works also when the MIPS wait
		instruction is used.
20060928	Re-implementing checks for coprocessor availability for MIPS
		cop0 instructions. (Thanks to Carl van Schaik for noticing the
		lack of cop0 availability checks.)
20060929	Implementing an instruction combination hack which treats
		NetBSD/pmax' idle loop as a wait-like instruction.
20060930	The ENTRYHI_R_MASK was missing in (at least) memory_mips_v2p.c,
		causing TLB lookups to sometimes succeed when they should have
		failed. (A big thank you to Juli Mallett for noticing the
		problem.)
		Adding disassembly support for more MIPS64 revision 2 opcodes
		(seb, seh, wsbh, jalr.hb, jr.hb, synci, ins, dins, dinsu,
		dinsm, dsbh, dshd, ror, dror, rorv, drorv, dror32). Also
		implementing seb, seh, dsbh, dshd, and wsbh.
		Implementing an instruction combination hack for Linux/pmax'
		idle loop, similar to the NetBSD/pmax case.
20061001	Changing the NetBSD/sgimips install instructions to extract
		files from an iso image, instead of downloading them via ftp.
20061002	More-than-31-bit userland addresses in memory_mips_v2p.c were
		not actually working; applying a fix from Carl van Schaik to
		enable them to work + making some other updates (adding kuseg
		support).
		Fixing hpcmips (vr41xx) timer initialization.
		Experimenting with O(n)->O(1) reduction in the MIPS TLB lookup
		loop. Seems to work both for R3000 and non-R3000.
20061003	Continuing a little on SH emulation (adding more control
		registers; mini-cleanup of memory_sh.c).
20061004	Beginning on a dev_rtc, a clock/timer device for the test
		machines; also adding a demo, and some documentation.
		Fixing a bug in SH "mov.w @(disp,pc),Rn" (the result wasn't
		sign-extended), and adding the addc and ldtlb instructions.
20061005	Contining on SH emulation: virtual to physical address
		translation, and a skeleton exception mechanism.
20061006	Adding more SH instructions (various loads and stores, rte,
		negc, muls.w, various privileged register-move instructions).
20061007	More SH instructions: various move instructions, trapa, div0s,
		float, fdiv, ftrc.
		Continuing on dev_rtc; removing the rtc demo.
20061008	Adding a dummy Dreamcast PROM module. (Homebrew Dreamcast
		programs using KOS libs need this.)
		Adding more SH instructions: "stc vbr,rn", rotl, rotr, fsca,
		fmul, fadd, various floating-point moves, etc. A 256-byte
		demo for Dreamcast runs :-)
20061012	Adding the SH "lds Rm,pr" and bsr instructions.
20061013	More SH instructions: "sts fpscr,rn", tas.b, and some more
		floating point instructions, cmp/str, and more moves.
		Adding a dummy dev_pvr (Dreamcast graphics controller).
20061014	Generalizing the expression evaluator (used in the built-in
		debugger) to support parentheses and +-*/%^&|.
20061015	Removing the experimental tlb index hint code in
		mips_memory_v2p.c, since it didn't really have any effect.
20061017	Minor SH updates; adding the "sts pr,Rn", fcmp/gt, fneg,
		frchg, and some other instructions. Fixing missing sign-
		extension in an 8-bit load instruction.
20061019	Adding a simple dev_dreamcast_rtc.
		Implementing memory-mapped access to the SH ITLB/UTLB arrays.
20061021	Continuing on various SH and Dreamcast things: sh4 timers,
		debug messages for dev_pvr, fixing some virtual address
		translation bugs, adding the bsrf instruction.
		The NetBSD/dreamcast GENERIC_MD kernel now reaches userland :)
		Adding a dummy dev_dreamcast_asic.c (not really useful yet).
		Implementing simple support for Store Queues.
		Beginning on the PVR Tile Accelerator.
20061022	Generalizing the PVR framebuffer to support off-screen drawing,
		multiple bit-depths, etc. (A small speed penalty, but most
		likely worth it.)
		Adding more SH instructions (mulu.w, fcmp/eq, fsub, fmac,
		fschg, and some more); correcting bugs in "fsca" and "float".
20061024	Adding the SH ftrv (matrix * vector) instruction. Marcus
		Comstedt's "tatest" example runs :) (wireframe only).
		Correcting disassembly for SH floating point instructions that
		use the xd* registers.
		Adding the SH fsts instruction.
		In memory_device_dyntrans_access(), only the currently used
		range is now invalidated, and not the entire device range.
20061025	Adding a dummy AVR32 cpu mode skeleton.
20061026	Various Dreamcast updates; beginning on a Maple bus controller.
20061027	Continuing on the Maple bus. A bogus Controller, Keyboard, and
		Mouse can now be detected by NetBSD and KOS homebrew programs.
		Cleaning up the SH4 Timer Management Unit, and beginning on
		SH4 interrupts.
		Implementing the Dreamcast SYSASIC.
20061028	Continuing on the SYSASIC.
		Adding the SH fsqrt instruction.
		memory_sh.c now actually scans the ITLB.
		Fixing a bug in dev_sh4.c, related to associative writes into
		the memory-mapped UTLB array. NetBSD/dreamcast now reaches
		userland stably, and prints the "Terminal type?" message :-]
		Implementing enough of the Dreamcast keyboard to make NetBSD
		accept it for input.
		Enabling SuperH for stable (non-development) builds.
		Adding NetBSD/dreamcast to the documentation, although it
		doesn't support root-on-nfs yet.
20061029	Changing usleep(1) calls in the debugger to to usleep(10000)
		(according to Brian Foley, this makes GXemul run better on
		MacOS X).
		Making the Maple "Controller" do something (enough to barely
		interact with dcircus.elf).
20061030-31	Some progress on the PVR. More test programs start running (but
		with strange output).
		Various other SH4-related updates.
20061102	Various Dreamcast and SH4 updates; more KOS demos run now.
20061104	Adding a skeleton dev_mb8696x.c (the Dreamcast's LAN adapter).
20061105	Continuing on the MB8696x; NetBSD/dreamcast detects it as mbe0.
		Testing for the release.

==============  RELEASE 0.4.3  ==============


1 dpavlin 32 /*
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    

  ViewVC Help
Powered by ViewVC 1.1.26