1 /*  Copyright 2005-2008 Lawrence Sebald
2 
3     This file is part of Yabause.
4 
5     Yabause is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     Yabause is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with Yabause; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18 */
19 
20 #include "perdc.h"
21 #include "../yabause.h"
22 #include "../yui.h"
23 #include "../vdp2.h"
24 
25 #include <dc/maple.h>
26 #include <dc/maple/controller.h>
27 
28 int PERDCInit(void);
29 void PERDCDeInit(void);
30 int PERDCHandleEvents(void);
31 void PERDCNothing(void);
32 u32 PERDCScan(u32 flags);
33 void PERDCKeyName(u32 key, char *name, int size);
34 
35 static PerPad_struct *pad1;
36 
37 PerInterface_struct PERDC = {
38     PERCORE_DC,
39     "Dreamcast Input Interface",
40     PERDCInit,
41     PERDCDeInit,
42     PERDCHandleEvents,
43     PERDCScan,
44     0,
45     PERDCNothing,
46     PERDCKeyName
47 };
48 
PERDCInit(void)49 int PERDCInit(void)	{
50     PerPortReset();
51     pad1 = PerPadAdd(&PORTDATA1);
52 	return 0;
53 }
54 
PERDCDeInit(void)55 void PERDCDeInit(void)	{
56 }
57 
PERDCHandleEvents(void)58 int PERDCHandleEvents(void)	{
59     maple_device_t *dev;
60 
61     dev = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
62     if(dev != NULL) {
63         cont_state_t *state = (cont_state_t *) maple_dev_status(dev);
64 
65         if(state != NULL)   {
66             if(state->buttons & CONT_DPAD_UP)
67                 *pad1->padbits &= 0xEF;
68             else
69                 *pad1->padbits |= 0x10;
70 
71             if(state->buttons & CONT_DPAD_DOWN)
72                 *pad1->padbits &= 0xDF;
73             else
74                 *pad1->padbits |= 0x20;
75 
76             if(state->buttons & CONT_DPAD_RIGHT)
77                 *pad1->padbits &= 0x7F;
78             else
79                 *pad1->padbits |= 0x80;
80 
81             if(state->buttons & CONT_DPAD_LEFT)
82                 *pad1->padbits &= 0xBF;
83             else
84                 *pad1->padbits |= 0x40;
85 
86             if(state->buttons & CONT_START)
87                 *pad1->padbits &= 0xF7;
88             else
89                 *pad1->padbits |= 0x08;
90 
91             if(state->buttons & CONT_A)
92                 *pad1->padbits &= 0xFB;
93             else
94                 *pad1->padbits |= 0x04;
95 
96             if(state->buttons & CONT_B)
97                 *pad1->padbits &= 0xFE;
98             else
99                 *pad1->padbits |= 0x01;
100 
101             if(state->buttons & CONT_X)
102                 *(pad1->padbits + 1) &= 0xBF;
103             else
104                 *(pad1->padbits + 1) |= 0x40;
105 
106             if(state->buttons & CONT_Y)
107                 *(pad1->padbits + 1) &= 0xDF;
108             else
109                 *(pad1->padbits + 1) |= 0x20;
110 
111             if(state->rtrig > 20)
112                 *(pad1->padbits + 1) &= 0x7F;
113             else
114                 *(pad1->padbits + 1) |= 0x80;
115 
116             if(state->ltrig > 20)
117                 *(pad1->padbits + 1) &= 0xF7;
118             else
119                 *(pad1->padbits + 1) |= 0x08;
120 
121             if(state->joyx > 20)
122                 *pad1->padbits &= 0xFD;
123             else
124                 *pad1->padbits |= 0x02;
125 
126             if(state->joyy > 20)
127                 *(pad1->padbits + 1) &= 0xEF;
128             else
129                 *(pad1->padbits + 1) |= 0x10;
130 
131         }
132     }
133 
134     YabauseExec();
135 
136 	return 0;
137 }
138 
PERDCNothing(void)139 void PERDCNothing(void) {
140     /* Nothing */
141 }
142 
PERDCScan(u32 flags)143 u32 PERDCScan(u32 flags) {
144     /* Nothing */
145     return 0;
146 }
147 
PERDCKeyName(u32 key,char * name,int size)148 void PERDCKeyName(u32 key, char *name, int size) {
149     snprintf(name, size, "%x", (unsigned int)key);
150 }
151