mapper fixes for ncpu, debug is broken atm
[fceu.git] / drivers / gp2x / input.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define JOY_A   1
22 #define JOY_B   2
23 #define JOY_SELECT      4
24 #define JOY_START       8
25 #define JOY_UP  0x10
26 #define JOY_DOWN        0x20
27 #define JOY_LEFT        0x40
28 #define JOY_RIGHT       0x80
29
30
31 #include "minimal.h"
32 extern int swapbuttons;
33 extern int scaled_display;
34 extern int FSkip_setting;
35
36 extern void SetVideoScaling(int pixels,int width,int height);
37 static INLINE long UpdateGamepadGP2X(void);
38
39
40
41 /* UsrInputType[] is user-specified.  InputType[] is current
42         (game loading can override user settings)
43 */
44 static int UsrInputType[2]={SI_GAMEPAD,SI_GAMEPAD};
45 static int InputType[2];
46
47 static int UsrInputTypeFC={SI_NONE};
48 static int InputTypeFC;
49
50 static uint32 JSreturn;
51 int NoWaiting=0;
52
53 static int powerpadsc[2][12];
54 static int powerpadside=0;
55
56
57 static uint32 MouseData[3];
58 static uint8 fkbkeys[0x48];
59 unsigned long lastpad=0;
60
61 void FCEUD_UpdateInput(void)
62 {
63   int t=0;
64   long lastpad2=lastpad;
65   long pad = UpdateGamepadGP2X();
66   t=1;
67   //  JSreturn=(JSreturn&0xFF000000)|(JSreturn&0xFF)|((JSreturn&0xFF0000)>>8)|((JSreturn&0xFF00)<<8);
68   if(gametype==GIT_FDS)
69   {
70     NoWaiting&=~1;
71         if ((pad & GP2X_PUSH) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_L)) && (!(pad & GP2X_R)) && (!(lastpad2 & GP2X_PUSH)))
72         {
73       DriverInterface(DES_FDSSELECT,0);
74         }
75         else if ((pad & GP2X_L) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_PUSH)) && (!(pad & GP2X_R))&& (!(lastpad2 & GP2X_L)))
76         {
77       DriverInterface(DES_FDSINSERT,0);
78         }
79         else if ((pad & GP2X_R) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_L)) && (!(pad & GP2X_PUSH)) && (!(lastpad2 & GP2X_R)))
80         {
81       DriverInterface(DES_FDSEJECT,0);
82         }
83   }
84
85 }
86
87
88 //#ifdef GP2X
89
90 extern void ResetNES(void);
91 extern void CleanSurface(void);
92
93 char soundvolmeter[21];
94 int soundvolIndex=0;
95 int L_count=0;
96 int R_count=0;
97 extern int CurrentState;
98 int TurboFireOff=1;
99 int TurboFireTop=0;  // 0 is none  // 1 is  A & X turbo  // 2 is Y & B turbo
100 int TurboFireBottom=0;
101 int turbo_toggle_A=0;
102 int turbo_toggle_B=0;
103
104 static void setsoundvol(int soundvolume)
105 {
106     //FCEUI_SetSoundVolume(soundvol);
107     // draw on screen :D
108     gp2x_sound_volume(soundvolume, soundvolume);
109     int meterval=soundvolume/5;
110     for (soundvolIndex=0; soundvolIndex < 20; soundvolIndex++)
111     {
112        if (soundvolIndex < meterval)
113        {
114           soundvolmeter[soundvolIndex]='*';
115        }
116        else
117        {
118           soundvolmeter[soundvolIndex]='_';
119        }
120     }
121     soundvolmeter[20]=0;
122     FCEU_DispMessage("|%s|", soundvolmeter);
123 }
124 /**
125  * GP2x joystick reader
126  *
127  */
128 static INLINE long UpdateGamepadGP2X(void)
129 {
130   uint32 JS=0;
131
132   unsigned long pad=gp2x_joystick_read();
133 #define down(b) (pad & GP2X_##b)
134 #define last_down(b) (lastpad & GP2X_##b)
135 #define L_down (pad & GP2X_L)
136 #define R_down (pad & GP2X_R)
137 #define last_L_down (lastpad & GP2X_L)
138 #define last_R_down (lastpad & GP2X_R)
139 #define shift      ((pad     & GP2X_PUSH) || ((pad     & (GP2X_VOL_UP|GP2X_VOL_DOWN)) == (GP2X_VOL_UP|GP2X_VOL_DOWN)))
140 #define last_shift ((lastpad & GP2X_PUSH) || ((lastpad & (GP2X_VOL_UP|GP2X_VOL_DOWN)) == (GP2X_VOL_UP|GP2X_VOL_DOWN)))
141
142   if (L_down && R_down && !(pad & GP2X_PUSH) && !(last_R_down && last_L_down))
143   {
144      ResetNES();
145      puts("Reset");
146      goto no_pad;
147   }
148
149   if (down(VOL_UP) && !down(VOL_DOWN))
150   {
151     soundvol+=1;
152     if (soundvol >= 100) soundvol=100;
153     //FCEUI_SetSoundVolume(soundvol);
154     setsoundvol(soundvol);
155   }
156   else if (down(VOL_DOWN) && !down(VOL_UP))
157   {
158     soundvol-=1;
159     if (soundvol < 0) soundvol=0;
160     //FCEUI_SetSoundVolume(soundvol);
161     setsoundvol(soundvol);
162   }
163
164   if (shift)
165   {
166     // only if it's something else then last time, and not moving around the joystick
167     if (!(pad & (GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT)))
168     {
169       if (down(SELECT))
170       {
171         if (last_down(SELECT) && last_shift)
172         {
173           // still pressed down from stretching from last one
174           goto no_pad;
175         }
176         scaled_display = !scaled_display;
177
178         if (scaled_display)
179         {
180           SetVideoScaling(320, 256, 240);
181         }
182         else
183         {
184           SetVideoScaling(320, 320, 240);
185         }
186
187         goto no_pad;
188       }
189       else if (L_down && R_down)
190       {
191         FCEUI_CloseGame();
192         puts("Quit");
193         goto no_pad;
194       }
195       else if (R_down && !(last_R_down && last_shift))
196       {
197        FCEUI_LoadState();
198        goto no_pad;
199       }
200       else if (L_down && !(last_L_down && last_shift))
201       {
202        FCEUI_SaveState();
203        goto no_pad;
204       }
205       else if (down(A) && !(last_down(A) && last_shift))
206       {
207        FSkip_setting--;
208        if (FSkip_setting < 0) {
209         FSkip_setting = -1;
210         FCEUI_DispMessage("Auto frameskip");
211        }
212        else
213         FCEUI_DispMessage("Frameskip: %i", FSkip_setting);
214        goto no_pad;
215       }
216       else if (down(Y) && !(last_down(Y) && last_shift))
217       {
218        FSkip_setting++;
219        if (FSkip_setting > 8) FSkip_setting = 8;
220        FCEUI_DispMessage("Frameskip: %i", FSkip_setting);
221        goto no_pad;
222       }
223     }
224   }
225
226   // r is toggle savestate
227   if (R_down)
228   {
229         if (last_R_down)
230         {
231           R_count++;
232       if ((R_count & 31)== 31)
233       {
234                 CurrentState=(CurrentState+1) % 10;
235                 FCEUI_DispMessage("Now Using Save State %d", CurrentState);
236           }
237         }
238   }
239   else
240   {
241     R_count=0;
242   }
243
244   // l is toggle turbo
245   if (L_down)
246   {
247         if (last_L_down)
248         {
249           L_count++;
250       if ((L_count & 31)== 31)
251       {
252        // 0 is none  // 1 is  Y & B turbo  // 2 is X & A turbo
253         if ((!TurboFireTop) && (!TurboFireBottom))
254         {
255                 // was off
256                 TurboFireTop=1;
257                 TurboFireBottom=0;
258                 if (swapbuttons)
259                 {
260                   FCEUI_DispMessage("Turbo A and Y");
261                 }
262                 else
263                 {
264                   FCEUI_DispMessage("Turbo Y and B");
265                 }
266         }
267         else if (TurboFireTop)
268         {
269                 TurboFireTop=0;
270                 TurboFireBottom=1;
271                 if (swapbuttons)
272                 {
273                   FCEUI_DispMessage("Turbo X and B");
274                 }
275                 else
276                 {
277                   FCEUI_DispMessage("Turbo A and X");
278                 }
279         }
280         else
281         {
282                 TurboFireTop=0;
283                 TurboFireBottom=0;
284                 FCEUI_DispMessage("Turbo Off");
285         }
286
287           }
288         }
289   }
290   else
291   {
292     L_count=0;
293   }
294
295   //unsigned long padTmp=0;
296   // shift the bits in
297   // up
298   //padTmp=(pad & GP2X_UP) ;  // 1 is 2^0,
299   JS |= ((pad & GP2X_UP) << (4-0));  // 0x10 is 2^4
300
301   //padTmp=(pad & GP2X_DOWN);  // 0x10 is 2^4,
302   JS |= ((pad & GP2X_DOWN) << (5-4));  // 0x20 is 2^5
303
304   //padTmp=(pad & GP2X_LEFT);  // 0x4 is 2^2,
305   JS |= ((pad & GP2X_LEFT) << (6-2));  // 0x40 is 2^6
306
307   //padTmp=(pad & GP2X_RIGHT);  // 0x40 is 2^6,
308   JS |= ((pad & GP2X_RIGHT) << (7-6));  // 0x80 is 2^7
309
310
311 #define  A_down (pad & GP2X_A)
312 #define  B_down (pad & GP2X_B)
313 #define  X_down (pad & GP2X_X)
314 #define  Y_down (pad & GP2X_Y)
315
316   // should be 2 cycles held, 1 cycle release
317   turbo_toggle_A=(turbo_toggle_A+1) % 3;
318   turbo_toggle_B=(turbo_toggle_B+1) % 3;
319
320    // 0 is none  // 1 is  Y & B turbo  // 2 is X & A turbo
321   // B or X are both considered A
322   //padTmp=B_down >> 13;  // 2^13,
323
324    if (!(TurboFireTop && (!turbo_toggle_A)))
325    {
326     JS |= ((B_down >> 13) << 0);  // 0x1 is 2^0
327    }
328    // A or Y are both considered B
329    //padTmp=A_down >> 12;  // 2^13,
330    if (!(TurboFireBottom && (!turbo_toggle_B)))
331    {
332     JS |= ((A_down >> 12) << 1);  // 0x2 is 2^1
333    }
334
335   if (swapbuttons)
336   {
337    //padTmp=X_down >> 14;  // 2^13,
338    if (!(TurboFireBottom && (!turbo_toggle_A)))
339    {
340 //    JS |= ((X_down >> 14) << 0);  // 0x1 is 2^0
341     JS |= ((Y_down >> 15) << 0);  // 0x1 is 2^0
342    }
343
344    //padTmp=Y_down >> 15;  // 2^13,
345    if (!(TurboFireTop && (!turbo_toggle_B)))
346    {
347     JS |= ((X_down >> 14) << 1);  // 0x2 is 2^1
348    }
349   }
350   else
351   {
352    //padTmp=X_down >> 14;  // 2^13,
353    if (!(TurboFireBottom && (!turbo_toggle_A)))
354    {
355     JS |= ((X_down >> 14) << 0);  // 0x1 is 2^0
356    }
357
358    //padTmp=Y_down >> 15;  // 2^13,
359    if (!(TurboFireTop && (!turbo_toggle_B)))
360    {
361     JS |= ((Y_down >> 15) << 1);  // 0x2 is 2^1
362    }
363   }
364
365   // select
366   //padTmp=(pad & GP2X_SELECT) >> 9;  // 0x40 is 2^9,
367   JS |= (((pad & GP2X_SELECT) >> 9) << 2);  // 0x4 is 2^2
368
369   // start
370   //padTmp=(pad & GP2X_START) >> 8;  //   2^8,
371   JS |= (((pad & GP2X_START) >> 8) << 3);  // 0x8 is 2^3
372
373
374   JSreturn = JS;
375   lastpad=pad;
376
377   return pad;
378   //JSreturn=(JS&0xFF000000)|(JS&0xFF)|((JS&0xFF0000)>>8)|((JS&0xFF00)<<8);
379 no_pad:
380   JSreturn=0;
381   lastpad=pad;
382   return 0;
383 }
384 //#endif
385
386
387 static void InitOtherInput(void)
388 {
389
390    void *InputDPtr;
391
392    int t;
393    int x;
394    int attrib;
395
396    for(t=0,x=0;x<2;x++)
397    {
398     attrib=0;
399     InputDPtr=0;
400     switch(InputType[x])
401     {
402       //case SI_POWERPAD:InputDPtr=&powerpadbuf[x];break;
403      case SI_GAMEPAD:InputDPtr=((uint8 *)&JSreturn)+(x<<1);break;
404      case SI_ARKANOID:InputDPtr=MouseData;t|=1;break;
405      case SI_ZAPPER:InputDPtr=MouseData;
406                                 t|=1;
407                                 attrib=1;
408                                 break;
409     }
410     FCEUI_SetInput(x,InputType[x],InputDPtr,attrib);
411    }
412
413    attrib=0;
414    InputDPtr=0;
415    switch(InputTypeFC)
416    {
417     case SIFC_SHADOW:InputDPtr=MouseData;t|=1;attrib=1;break;
418     case SIFC_ARKANOID:InputDPtr=MouseData;t|=1;break;
419     case SIFC_FKB:InputDPtr=fkbkeys;break;
420    }
421
422    FCEUI_SetInputFC(InputTypeFC,InputDPtr,attrib);
423    FCEUI_DisableFourScore(eoptions&EO_NOFOURSCORE);
424
425    if(t && !(inited&16))
426    {
427     InitMouse();
428     inited|=16;
429    }
430 }