initial fce ultra 0.81 import
[fceu.git] / drivers / cli / dos-joystick.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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include <dpmi.h>
28 #include <sys/farptr.h>
29 #include <go32.h>
30 #include <pc.h>
31
32 #include "dos.h"
33 #include "dos-joystick.h"
34
35 #define JOY_A   1
36 #define JOY_B   2
37 #define JOY_SELECT      4
38 #define JOY_START       8
39 #define JOY_UP  0x10
40 #define JOY_DOWN        0x20
41 #define JOY_LEFT        0x40
42 #define JOY_RIGHT       0x80
43
44 int joy=0;
45 int joyBMap[4];
46
47 static int32 joybuttons=0;
48 static uint32 joyx=0;
49 static uint32 joyy=0;
50 static uint32 joyxcenter;
51 static uint32 joyycenter;
52
53 static void ConfigJoystick(void);
54 volatile int soundjoyer=0;
55 volatile int soundjoyeron=0;
56
57 /* Crude method to detect joystick. */
58 static int DetectJoystick(void)
59 {
60  uint8 b;
61
62  outportb(0x201,0);
63  b=inportb(0x201);
64  sleep(1);
65  if((inportb(0x201)&3)==(b&3))
66   return 0;
67  else
68   return 1;
69 }
70
71 void UpdateJoyData(void)
72 {
73  uint32 xc,yc;
74
75
76  joybuttons=((inportb(0x201)&0xF0)^0xF0)>>4;
77
78  xc=yc=0;
79
80  {
81   outportb(0x201,0);
82
83   for(;;)
84   {
85    uint8 b;
86
87    b=inportb(0x201);
88    if(!(b&3))
89     break;
90    if(b&1) xc++;
91    if(b&2) yc++;
92   }
93  }
94
95  joyx=xc;
96  joyy=yc;
97 }
98
99 uint32 GetJSOr(void)
100 {
101         int y;
102         unsigned long ret;
103         ret=0;
104
105         if(!soundo)
106          UpdateJoyData();
107         for(y=0;y<4;y++)
108          if(joybuttons&joyBMap[y]) ret|=(1<<y)<<((joy-1)<<3);
109
110         if(joyx<=joyxcenter*.25) ret|=JOY_LEFT<<((joy-1)<<3);
111         else if(joyx>=joyxcenter*1.75) ret|=JOY_RIGHT<<((joy-1)<<3);
112         if(joyy<=joyycenter*.25) ret|=JOY_UP<<((joy-1)<<3);
113         else if(joyy>=joyycenter*1.75) ret|=JOY_DOWN<<((joy-1)<<3);
114
115         return ret;
116 }
117
118 int InitJoysticks(void)
119 {
120         if(!joy) return(0);
121         if(!DetectJoystick())
122         {
123          printf("Joystick not detected!\n");
124          joy=0;
125          return 0;
126         }
127         if(soundo)
128         {
129          soundjoyeron=1;
130          while(!soundjoyer);
131         }
132         else
133          UpdateJoyData();
134
135         joyxcenter=joyx;
136         joyycenter=joyy;
137
138         if(!(joyBMap[0]|joyBMap[1]|joyBMap[2]|joyBMap[3]))
139          ConfigJoystick();
140         return(1);
141 }
142
143 static void BConfig(int b)
144 {
145   int c=0;
146   uint32 st=time(0);
147
148   while(time(0)< (st+4) )
149   {
150    if(!soundo)
151     UpdateJoyData();
152    if(joybuttons) c=joybuttons;
153    else if(c && !joybuttons)
154    {
155     joyBMap[b]=c;
156     break;
157    }
158
159   }
160 }
161
162 void KillJoysticks(void)
163 {
164
165 }
166
167 static void ConfigJoystick(void)
168 {
169  static char *genb="** Press button for ";
170
171  printf("\n\n Joystick button configuration:\n\n");
172  printf("   Push and release the button to map to the virtual joystick.\n");
173  printf("   If you do not wish to assign a button, wait a few seconds\n");
174  printf("   and the configuration will continue.\n\n");
175  printf("   Press enter to continue...\n");
176  getchar();
177                                                         
178  printf("%s\"Select\".\n",genb);
179  BConfig(2);
180
181  printf("%s\"Start\".\n",genb);
182  BConfig(3);
183
184  printf("%s\"B\".\n",genb);
185  BConfig(1);
186
187  printf("%s\"A\".\n",genb);
188  BConfig(0);
189 }
190