aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ioemu/iodev/gameport.cc
blob: 9adefa6551076fd217e803d900337c133d6e6ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/////////////////////////////////////////////////////////////////////////
// $Id: gameport.cc,v 1.5 2003/12/29 21:48:56 cbothamy Exp $
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003  MandrakeSoft S.A.
//
//    MandrakeSoft S.A.
//    43, rue d'Aboukir
//    75002 Paris - France
//    http://www.linux-mandrake.com/
//    http://www.mandrakesoft.com/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

//
// Standard PC gameport
//

// Define BX_PLUGGABLE in files that can be compiled into plugins.  For
// platforms that require a special tag on exported symbols, BX_PLUGGABLE 
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE

#include "bochs.h"

#ifdef __linux__

#include <linux/joystick.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#elif defined(WIN32)

#ifndef JOY_BUTTON1
#define JOY_BUTTON1 1
#define JOY_BUTTON2 2
UINT STDCALL joyGetPos(UINT, LPJOYINFO);
#endif

#define JOYSTICKID1 0

#endif

#define LOG_THIS theGameport->

bx_gameport_c *theGameport = NULL;

  int
libgameport_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
{
  theGameport = new bx_gameport_c ();
  bx_devices.pluginGameport = theGameport;
  BX_REGISTER_DEVICE_DEVMODEL(plugin, type, theGameport, BX_PLUGIN_GAMEPORT);
  return(0); // Success
}

  void
libgameport_LTX_plugin_fini(void)
{
}

bx_gameport_c::bx_gameport_c(void)
{
  put("GAME");
  settype(EXTFPUIRQLOG);
}

bx_gameport_c::~bx_gameport_c(void)
{
  if (joyfd >= 0) close(joyfd);
  BX_DEBUG(("Exit."));
}


  void
bx_gameport_c::init(void)
{
  // Allocate the gameport IO address range 0x200..0x207
  for (unsigned addr=0x200; addr<0x208; addr++) {
    DEV_register_ioread_handler(this, read_handler, addr, "Gameport", 1);
    DEV_register_iowrite_handler(this, write_handler, addr, "Gameport", 1);
  }

  BX_GAMEPORT_THIS port = 0xf0;
  BX_GAMEPORT_THIS write_usec = 0;
  BX_GAMEPORT_THIS timer_x = 0;
  BX_GAMEPORT_THIS timer_y = 0;

#ifdef __linux__
  BX_GAMEPORT_THIS joyfd = open("/dev/input/js0", O_RDONLY);
  if (BX_GAMEPORT_THIS joyfd >= 0) {
    for (unsigned i=0; i<4; i++) poll_joydev();
  }
#elif defined(WIN32)
  JOYINFO joypos;
  if (joyGetPos(JOYSTICKID1, &joypos) == JOYERR_NOERROR) {
    BX_GAMEPORT_THIS joyfd = 1;
  } else {
    BX_GAMEPORT_THIS joyfd = -1;
  }
#else
  BX_GAMEPORT_THIS joyfd = -1;
#endif
}

  void
bx_gameport_c::reset(unsigned type)
{
  // nothing for now
}

  void
bx_gameport_c::poll_joydev(void)
{
#ifdef __linux__
  struct js_event e;
  fd_set joyfds;
  struct timeval tv;

  memset(&tv, 0, sizeof(tv));
  FD_ZERO(&joyfds);
  FD_SET(BX_GAMEPORT_THIS joyfd, &joyfds);
  e.type = 0;
  if (select(BX_GAMEPORT_THIS joyfd+1, &joyfds, NULL, NULL, &tv)) {
    read(BX_GAMEPORT_THIS joyfd, &e, sizeof(struct js_event));
    if (e.type & JS_EVENT_BUTTON) {
      if (e.value == 1) {
        BX_GAMEPORT_THIS port &= ~(0x10 << e.number);
      } else {
        BX_GAMEPORT_THIS port |= (0x10 << e.number);
      }
    }
    if (e.type & JS_EVENT_AXIS) {
      if (e.number == 0) {
        BX_GAMEPORT_THIS delay_x = 25 + ((e.value + 0x8000) / 60);
      }
      if (e.number == 1) {
        BX_GAMEPORT_THIS delay_y = 25 + ((e.value + 0x8000) / 62);
      }
    }
  }
#elif defined(WIN32)
  JOYINFO joypos;
  if (joyGetPos(JOYSTICKID1, &joypos) == JOYERR_NOERROR) {
    if (joypos.wButtons & JOY_BUTTON1) {
      BX_GAMEPORT_THIS port &= ~0x10;
    } else {
      BX_GAMEPORT_THIS port |= 0x10;
    }
    if (joypos.wButtons & JOY_BUTTON2) {
      BX_GAMEPORT_THIS port &= ~0x20;
    } else {
      BX_GAMEPORT_THIS port |= 0x20;
    }
    BX_GAMEPORT_THIS delay_x = 25 + (joypos.wXpos / 60);
    BX_GAMEPORT_THIS delay_y = 25 + (joypos.wYpos / 60);
  }
#endif
}


  // static IO port read callback handler
  // redirects to non-static class handler to avoid virtual functions

  Bit32u
bx_gameport_c::read_handler(void *this_ptr, Bit32u address, unsigned io_len)
{
#if !BX_USE_GAME_SMF
  bx_gameport_c *class_ptr = (bx_gameport_c *) this_ptr;

  return( class_ptr->read(address, io_len) );
}


  Bit32u
bx_gameport_c::read(Bit32u address, unsigned io_len)
{
#else
  UNUSED(this_ptr);
#endif // !BX_USE_GAME_SMF
  Bit64u usec;

  if (BX_GAMEPORT_THIS joyfd >= 0) {
    poll_joydev();
    usec = bx_pc_system.time_usec();
    if (BX_GAMEPORT_THIS timer_x) {
      if ((usec - BX_GAMEPORT_THIS write_usec) >= BX_GAMEPORT_THIS delay_x) {
        BX_GAMEPORT_THIS port &= 0xfe;
        BX_GAMEPORT_THIS timer_x = 0;
      }
    }
    if (BX_GAMEPORT_THIS timer_y) {
      if ((usec - BX_GAMEPORT_THIS write_usec) >= BX_GAMEPORT_THIS delay_y) {
        BX_GAMEPORT_THIS port &= 0xfd;
        BX_GAMEPORT_THIS timer_y = 0;
      }
    }
  } else {
    BX_DEBUG(("read: joystick not present"));
  }
  return BX_GAMEPORT_THIS port;
}


  // static IO port write callback handler
  // redirects to non-static class handler to avoid virtual functions

  void
bx_gameport_c::write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len)
{
#if !BX_USE_GAME_SMF
  bx_gameport_c *class_ptr = (bx_gameport_c *) this_ptr;

  class_ptr->write(address, value, io_len);
}

  void
bx_gameport_c::write(Bit32u address, Bit32u value, unsigned io_len)
{
#else
  UNUSED(this_ptr);
#endif // !BX_USE_GAME_SMF

  BX_GAMEPORT_THIS write_usec = bx_pc_system.time_usec();
  BX_GAMEPORT_THIS timer_x = 1;
  BX_GAMEPORT_THIS timer_y = 1;
  BX_GAMEPORT_THIS port |= 0x0f;
}