src/joystick/mint/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 10 Mar 2002 03:33:59 +0000
changeset 305 9c6613983e85
parent 302 8a86bdf34f0f
child 507 7853b6af5957
permissions -rw-r--r--
Atari port cleanups from Patrice
slouken@302
     1
/*
slouken@302
     2
    SDL - Simple DirectMedia Layer
slouken@302
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@302
     4
slouken@302
     5
    This library is free software; you can redistribute it and/or
slouken@302
     6
    modify it under the terms of the GNU Library General Public
slouken@302
     7
    License as published by the Free Software Foundation; either
slouken@302
     8
    version 2 of the License, or (at your option) any later version.
slouken@302
     9
slouken@302
    10
    This library is distributed in the hope that it will be useful,
slouken@302
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@302
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@302
    13
    Library General Public License for more details.
slouken@302
    14
slouken@302
    15
    You should have received a copy of the GNU Library General Public
slouken@302
    16
    License along with this library; if not, write to the Free
slouken@302
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@302
    18
slouken@302
    19
    Sam Lantinga
slouken@302
    20
    slouken@libsdl.org
slouken@302
    21
*/
slouken@302
    22
slouken@302
    23
#ifdef SAVE_RCSID
slouken@302
    24
static char rcsid =
slouken@302
    25
 "@(#) $Id$";
slouken@302
    26
#endif
slouken@302
    27
slouken@302
    28
/*
slouken@302
    29
 *	Atari Joystick/Joypad drivers
slouken@302
    30
 *
slouken@302
    31
 *	Patrice Mandin
slouken@302
    32
 */
slouken@302
    33
slouken@305
    34
#include <stdio.h>
slouken@302
    35
#include <stdlib.h>
slouken@302
    36
#include <string.h>
slouken@302
    37
slouken@302
    38
#include <sys/cookie.h>
slouken@302
    39
#include <mint/osbind.h>
slouken@302
    40
slouken@302
    41
#include "SDL_types.h"
slouken@302
    42
#include "SDL_error.h"
slouken@302
    43
#include "SDL_joystick.h"
slouken@302
    44
#include "SDL_sysjoystick.h"
slouken@302
    45
#include "SDL_joystick_c.h"
slouken@302
    46
slouken@305
    47
#include "SDL_ikbdinterrupt_s.h"
slouken@305
    48
#include "SDL_xbiosevents_c.h"
slouken@305
    49
#include "SDL_xbiosinterrupt_s.h"
slouken@302
    50
slouken@302
    51
/*--- Const ---*/
slouken@302
    52
slouken@302
    53
/* We can have:
slouken@305
    54
	1 joystick on IKBD port 1, read via hardware I/O
slouken@305
    55
      or same joystick on IKBD port 1, read via xbios
slouken@302
    56
	2 joypads on ports A,B
slouken@302
    57
	  or 4 joysticks on joypads ports A,B
slouken@302
    58
	  or 1 lightpen on joypad port A
slouken@302
    59
	  or 2 analog paddles on joypads ports A,B
slouken@302
    60
	2 joysticks on parallel port
slouken@302
    61
*/
slouken@302
    62
slouken@302
    63
enum {
slouken@302
    64
	IKBD_JOY1=0,
slouken@305
    65
	XBIOS_JOY1,
slouken@302
    66
	PORTA_PAD,
slouken@302
    67
	PORTB_PAD,
slouken@302
    68
	PORTA_JOY0,
slouken@302
    69
	PORTA_JOY1,
slouken@302
    70
	PORTB_JOY0,
slouken@302
    71
	PORTB_JOY1,
slouken@302
    72
	PORTA_LP,
slouken@302
    73
	PORTA_ANPAD,
slouken@302
    74
	PORTB_ANPAD,
slouken@302
    75
#if 0
slouken@302
    76
	PARA_JOY0,
slouken@302
    77
	PARA_JOY1,
slouken@302
    78
#endif
slouken@302
    79
	MAX_JOYSTICKS
slouken@302
    80
};
slouken@302
    81
slouken@302
    82
enum {
slouken@302
    83
	MCH_ST=0,
slouken@302
    84
	MCH_STE,
slouken@302
    85
	MCH_TT,
slouken@302
    86
	MCH_F30
slouken@302
    87
};
slouken@302
    88
slouken@302
    89
/*	Joypad buttons
slouken@302
    90
 *		Procontroller note:
slouken@302
    91
 *			L,R are connected to 4,6
slouken@302
    92
 *			X,Y,Z are connected to 7,8,9
slouken@302
    93
 */
slouken@302
    94
slouken@302
    95
enum {
slouken@302
    96
	JP_UP=0,	JP_DOWN,	JP_LEFT,	JP_RIGHT,
slouken@302
    97
	JP_KPMULT,	JP_KP7,		JP_KP4,		JP_KP1,
slouken@302
    98
	JP_KP0,		JP_KP8,		JP_KP5,		JP_KP2,
slouken@302
    99
	JP_KPNUM,	JP_KP9,		JP_KP6,		JP_KP3,
slouken@302
   100
	JP_PAUSE,	JP_FIRE0,	JP_UNDEF0,	JP_FIRE1,
slouken@302
   101
	JP_UNDEF1,	JP_FIRE2,	JP_UNDEF2,	JP_OPTION
slouken@302
   102
};
slouken@302
   103
slouken@302
   104
#define JP_NUM_BUTTONS 17
slouken@302
   105
slouken@302
   106
#define PORT_JS_RIGHT	(1<<0)
slouken@302
   107
#define PORT_JS_LEFT	(1<<1)
slouken@302
   108
#define PORT_JS_DOWN	(1<<2)
slouken@302
   109
#define PORT_JS_UP		(1<<3)
slouken@302
   110
#define PORT_JS_FIRE	(1<<4)
slouken@302
   111
slouken@302
   112
/*--- Types ---*/
slouken@302
   113
slouken@302
   114
typedef struct {
slouken@302
   115
	SDL_bool enabled;
slouken@302
   116
	unsigned char *name;
slouken@302
   117
	Uint32 prevstate;
slouken@302
   118
} atarijoy_t;
slouken@302
   119
slouken@302
   120
/*--- Variables ---*/
slouken@302
   121
slouken@302
   122
static atarijoy_t atarijoysticks[MAX_JOYSTICKS]={
slouken@302
   123
	{SDL_FALSE,"IKBD joystick port 1",0},
slouken@305
   124
	{SDL_FALSE,"Xbios joystick port 1",0},
slouken@302
   125
	{SDL_FALSE,"Joypad port A",0},
slouken@302
   126
	{SDL_FALSE,"Joypad port B",0},
slouken@302
   127
	{SDL_FALSE,"Joystick 0 port A",0},
slouken@302
   128
	{SDL_FALSE,"Joystick 1 port A",0},
slouken@302
   129
	{SDL_FALSE,"Joystick 0 port B",0},
slouken@302
   130
	{SDL_FALSE,"Joystick 1 port B",0},
slouken@302
   131
	{SDL_FALSE,"Lightpen port A",0},
slouken@302
   132
	{SDL_FALSE,"Analog paddle port A",0},
slouken@302
   133
	{SDL_FALSE,"Analog paddle port B",0}
slouken@302
   134
#if 0
slouken@302
   135
	,{SDL_FALSE,"Joystick 0 parallel port",0},
slouken@302
   136
	{SDL_FALSE,"Joystick 1 parallel port",0}
slouken@302
   137
#endif
slouken@302
   138
};
slouken@302
   139
slouken@302
   140
static const int jp_buttons[JP_NUM_BUTTONS]={
slouken@305
   141
	JP_FIRE0,	JP_FIRE1,	JP_FIRE2,	JP_PAUSE,
slouken@305
   142
	JP_OPTION,	JP_KPMULT,	JP_KPNUM,	JP_KP0,
slouken@305
   143
	JP_KP1,		JP_KP2,		JP_KP3,		JP_KP4,
slouken@305
   144
	JP_KP5,		JP_KP6,		JP_KP7,		JP_KP8,
slouken@305
   145
	JP_KP9
slouken@302
   146
};
slouken@302
   147
slouken@302
   148
static SDL_bool joypad_ports_enabled=SDL_FALSE;
slouken@302
   149
slouken@302
   150
/* Updated joypad ports */
slouken@302
   151
static Uint16 jp_paddles[4];
slouken@302
   152
static Uint16 jp_lightpens[2];
slouken@302
   153
static Uint16 jp_directions;
slouken@302
   154
static Uint16 jp_fires;
slouken@302
   155
static Uint32 jp_joypads[2];
slouken@302
   156
slouken@302
   157
/*--- Functions prototypes ---*/
slouken@302
   158
slouken@302
   159
static int GetEnabledAtariJoystick(int index);
slouken@302
   160
static void UpdateJoypads(void);
slouken@302
   161
slouken@302
   162
/*--- Functions ---*/
slouken@302
   163
slouken@302
   164
int SDL_SYS_JoystickInit(void)
slouken@302
   165
{
slouken@302
   166
	int i;
slouken@302
   167
	unsigned long cookie_mch;
slouken@302
   168
	const char *envr=getenv("SDL_JOYSTICK_ATARI");
slouken@302
   169
	
slouken@302
   170
#define TEST_JOY_ENABLED(env,idstring,num) \
slouken@302
   171
	if (strstr(env,idstring"-off")) { \
slouken@302
   172
		atarijoysticks[num].enabled=SDL_FALSE; \
slouken@302
   173
	} \
slouken@302
   174
	if (strstr(env,idstring"-on")) { \
slouken@302
   175
		atarijoysticks[num].enabled=SDL_TRUE; \
slouken@302
   176
	}
slouken@302
   177
slouken@302
   178
	/* Cookie _MCH present ? if not, assume ST machine */
slouken@302
   179
	if (Getcookie(C__MCH, &cookie_mch) != C_FOUND) {
slouken@302
   180
		cookie_mch = MCH_ST << 16;
slouken@302
   181
	}
slouken@302
   182
slouken@302
   183
	/* Enable some default joysticks */
slouken@302
   184
	if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
slouken@302
   185
		(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@305
   186
		atarijoysticks[IKBD_JOY1].enabled=(SDL_AtariIkbd_enabled!=0);
slouken@302
   187
	}
slouken@302
   188
	if ((cookie_mch == MCH_STE<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   189
		atarijoysticks[PORTA_PAD].enabled=SDL_TRUE;
slouken@302
   190
		atarijoysticks[PORTB_PAD].enabled=SDL_TRUE;
slouken@302
   191
	}
slouken@305
   192
	if (!atarijoysticks[IKBD_JOY1].enabled) {
slouken@305
   193
		atarijoysticks[XBIOS_JOY1].enabled=(SDL_AtariXbios_enabled!=0);
slouken@305
   194
	}
slouken@302
   195
slouken@302
   196
	/* Read environment for joysticks to enable */
slouken@302
   197
	if (envr) {
slouken@302
   198
		/* IKBD on any Atari, maybe clones */
slouken@302
   199
		if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
slouken@302
   200
			(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@305
   201
			if (SDL_AtariIkbd_enabled!=0) {
slouken@305
   202
				TEST_JOY_ENABLED(envr, "ikbd-joy1", IKBD_JOY1);
slouken@302
   203
			}
slouken@302
   204
		}
slouken@302
   205
		/* Joypads ports only on STE and Falcon */
slouken@302
   206
		if ((cookie_mch == MCH_STE<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   207
			TEST_JOY_ENABLED(envr, "porta-pad", PORTA_PAD);
slouken@302
   208
			if (!atarijoysticks[PORTA_PAD].enabled) {
slouken@302
   209
				TEST_JOY_ENABLED(envr, "porta-joy0", PORTA_JOY0);
slouken@302
   210
				TEST_JOY_ENABLED(envr, "porta-joy1", PORTA_JOY1);
slouken@302
   211
				if (!(atarijoysticks[PORTA_JOY0].enabled) && !(atarijoysticks[PORTA_JOY1].enabled)) {
slouken@302
   212
					TEST_JOY_ENABLED(envr, "porta-lp", PORTA_LP);
slouken@302
   213
					if (!atarijoysticks[PORTA_LP].enabled) {
slouken@302
   214
						TEST_JOY_ENABLED(envr, "porta-anpad", PORTA_ANPAD);
slouken@302
   215
					}
slouken@302
   216
				}
slouken@302
   217
			}
slouken@302
   218
slouken@302
   219
			TEST_JOY_ENABLED(envr, "portb-pad", PORTB_PAD);
slouken@302
   220
			if (!atarijoysticks[PORTB_PAD].enabled) {
slouken@302
   221
				TEST_JOY_ENABLED(envr, "portb-joy0", PORTB_JOY0);
slouken@302
   222
				TEST_JOY_ENABLED(envr, "portb-joy1", PORTB_JOY1);
slouken@302
   223
				if (!(atarijoysticks[PORTB_JOY0].enabled) && !(atarijoysticks[PORTB_JOY1].enabled)) {
slouken@302
   224
					TEST_JOY_ENABLED(envr, "portb-anpad", PORTB_ANPAD);
slouken@302
   225
				}
slouken@302
   226
			}
slouken@302
   227
		}
slouken@305
   228
slouken@305
   229
		if (!atarijoysticks[IKBD_JOY1].enabled) {
slouken@305
   230
			if (SDL_AtariXbios_enabled!=0) {
slouken@305
   231
				TEST_JOY_ENABLED(envr, "xbios-joy1", XBIOS_JOY1);
slouken@305
   232
			}
slouken@305
   233
		}
slouken@302
   234
#if 0
slouken@302
   235
		/* Parallel port on any Atari, maybe clones */
slouken@302
   236
		if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
slouken@302
   237
			(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   238
			TEST_JOY_ENABLED(envr, "para-joy0", PARA_JOY0);
slouken@302
   239
			TEST_JOY_ENABLED(envr, "para-joy1", PARA_JOY1);
slouken@302
   240
		}
slouken@302
   241
#endif
slouken@302
   242
	}
slouken@302
   243
slouken@302
   244
	/* Need to update joypad ports ? */
slouken@302
   245
	joypad_ports_enabled=SDL_FALSE;
slouken@302
   246
	for (i=PORTA_PAD;i<=PORTB_ANPAD;i++) {
slouken@302
   247
		if (atarijoysticks[i].enabled) {
slouken@302
   248
			joypad_ports_enabled=SDL_TRUE;
slouken@302
   249
			break;
slouken@302
   250
		}
slouken@302
   251
	}
slouken@302
   252
slouken@302
   253
	SDL_numjoysticks = 0;
slouken@302
   254
	for (i=0;i<MAX_JOYSTICKS;i++) {
slouken@302
   255
		if (atarijoysticks[i].enabled) {
slouken@302
   256
			++SDL_numjoysticks;
slouken@302
   257
		}
slouken@302
   258
	}
slouken@302
   259
slouken@302
   260
	return(SDL_numjoysticks);
slouken@302
   261
}
slouken@302
   262
slouken@302
   263
static int GetEnabledAtariJoystick(int index)
slouken@302
   264
{
slouken@302
   265
	int i,j;
slouken@302
   266
slouken@302
   267
	/* Return the nth'index' enabled atari joystick */
slouken@302
   268
	j=0;
slouken@302
   269
	for (i=0;i<MAX_JOYSTICKS;i++) {
slouken@305
   270
		if (!atarijoysticks[i].enabled) {
slouken@305
   271
			continue;
slouken@302
   272
		}
slouken@305
   273
slouken@305
   274
		if (j==index) {
slouken@305
   275
			break;
slouken@305
   276
		}
slouken@305
   277
slouken@305
   278
		++j;
slouken@302
   279
	}
slouken@302
   280
	if (i==MAX_JOYSTICKS)
slouken@302
   281
		return -1;
slouken@302
   282
slouken@302
   283
	return i;
slouken@302
   284
}
slouken@302
   285
slouken@302
   286
const char *SDL_SYS_JoystickName(int index)
slouken@302
   287
{
slouken@302
   288
	int numjoystick;
slouken@302
   289
slouken@302
   290
	numjoystick=GetEnabledAtariJoystick(index);
slouken@302
   291
	if (numjoystick==-1)
slouken@302
   292
		return NULL;
slouken@302
   293
slouken@302
   294
	return(atarijoysticks[numjoystick].name);
slouken@302
   295
}
slouken@302
   296
slouken@302
   297
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@302
   298
{
slouken@302
   299
	int numjoystick;
slouken@302
   300
	
slouken@302
   301
	numjoystick=GetEnabledAtariJoystick(joystick->index);
slouken@302
   302
	if (numjoystick==-1)
slouken@302
   303
		return -1;
slouken@302
   304
	
slouken@302
   305
	if ((numjoystick==PORTA_PAD) || (numjoystick==PORTB_PAD)) {
slouken@302
   306
		joystick->nbuttons=JP_NUM_BUTTONS;
slouken@302
   307
	} else if ((numjoystick==PORTA_LP) || (numjoystick==PORTA_ANPAD) ||
slouken@302
   308
				(numjoystick==PORTB_ANPAD)) {
slouken@302
   309
		joystick->nbuttons=2;
slouken@302
   310
	} else {
slouken@302
   311
		joystick->nbuttons=1;
slouken@302
   312
	}
slouken@302
   313
	joystick->naxes=2;
slouken@302
   314
	joystick->nballs=0;
slouken@302
   315
	joystick->nhats=0;
slouken@302
   316
slouken@302
   317
	return(0);
slouken@302
   318
}
slouken@302
   319
slouken@302
   320
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@302
   321
{
slouken@302
   322
	int numjoystick;
slouken@302
   323
	Uint32 curstate,prevstate;
slouken@302
   324
	Sint16 curaxis;
slouken@302
   325
	
slouken@302
   326
	numjoystick=GetEnabledAtariJoystick(joystick->index);
slouken@302
   327
	if (numjoystick==-1)
slouken@302
   328
		return;
slouken@302
   329
slouken@302
   330
	prevstate = atarijoysticks[numjoystick].prevstate;
slouken@302
   331
slouken@302
   332
	if (joypad_ports_enabled) {
slouken@302
   333
		Supexec(UpdateJoypads);
slouken@302
   334
	}
slouken@302
   335
slouken@302
   336
	switch (numjoystick) {
slouken@302
   337
		case IKBD_JOY1:
slouken@305
   338
		case XBIOS_JOY1:
slouken@302
   339
			{
slouken@305
   340
				curstate = 0;
slouken@305
   341
slouken@305
   342
				if (numjoystick==IKBD_JOY1) {
slouken@305
   343
					curstate = SDL_AtariIkbd_joystick & 0xff;
slouken@305
   344
				}
slouken@305
   345
				if (numjoystick==XBIOS_JOY1) {
slouken@305
   346
					curstate = SDL_AtariXbios_joystick & 0xff;
slouken@305
   347
				}
slouken@305
   348
slouken@302
   349
				if (curstate != prevstate) {
slouken@302
   350
					/* X axis */
slouken@302
   351
					if ((curstate & (IKBD_JOY_LEFT|IKBD_JOY_RIGHT)) != (prevstate & (IKBD_JOY_LEFT|IKBD_JOY_RIGHT))) {
slouken@302
   352
						curaxis=0;
slouken@302
   353
						if (curstate & IKBD_JOY_LEFT) {
slouken@302
   354
							curaxis=-128;
slouken@302
   355
						} else if (curstate & IKBD_JOY_RIGHT) {
slouken@302
   356
							curaxis=127;
slouken@302
   357
						}					
slouken@302
   358
						SDL_PrivateJoystickAxis(joystick,0,curaxis);
slouken@302
   359
					}
slouken@302
   360
					/* Y axis */
slouken@302
   361
					if ((curstate & (IKBD_JOY_UP|IKBD_JOY_DOWN)) != (prevstate & (IKBD_JOY_UP|IKBD_JOY_DOWN))) {
slouken@302
   362
						curaxis=0;
slouken@302
   363
						if (curstate & IKBD_JOY_UP) {
slouken@302
   364
							curaxis=-128;
slouken@302
   365
						} else if (curstate & IKBD_JOY_DOWN) {
slouken@302
   366
							curaxis=127;
slouken@302
   367
						}					
slouken@302
   368
						SDL_PrivateJoystickAxis(joystick,1,curaxis);
slouken@302
   369
					}
slouken@302
   370
					/* Button */
slouken@302
   371
					if ((curstate & IKBD_JOY_FIRE) && !(prevstate & IKBD_JOY_FIRE)) {
slouken@302
   372
						SDL_PrivateJoystickButton(joystick,0,SDL_PRESSED);
slouken@302
   373
					}
slouken@302
   374
					if (!(curstate & IKBD_JOY_FIRE) && (prevstate & IKBD_JOY_FIRE)) {
slouken@302
   375
						SDL_PrivateJoystickButton(joystick,0,SDL_RELEASED);
slouken@302
   376
					}
slouken@302
   377
				}
slouken@305
   378
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   379
			}
slouken@302
   380
			break;
slouken@302
   381
		case PORTA_PAD:
slouken@302
   382
		case PORTB_PAD:
slouken@302
   383
			{
slouken@302
   384
				int numjoypad,i;
slouken@302
   385
				
slouken@302
   386
				numjoypad=0;
slouken@302
   387
/*				if (numjoystick==PORTA_PAD) numjoypad=0;*/
slouken@302
   388
				if (numjoystick==PORTB_PAD) numjoypad=1;
slouken@302
   389
				
slouken@302
   390
				curstate=jp_joypads[numjoypad];
slouken@302
   391
				if (curstate!=prevstate) {
slouken@302
   392
					/* X axis */
slouken@302
   393
					if ((curstate & ((1<<JP_LEFT)|(1<<JP_RIGHT))) != (prevstate & ((1<<JP_LEFT)|(1<<JP_RIGHT)))) {
slouken@302
   394
						curaxis=0;
slouken@302
   395
						if (curstate & (1<<JP_LEFT)) {
slouken@302
   396
							curaxis=-128;
slouken@302
   397
						} else if (curstate & (1<<JP_RIGHT)) {
slouken@302
   398
							curaxis=127;
slouken@302
   399
						}					
slouken@302
   400
						SDL_PrivateJoystickAxis(joystick,0,curaxis);
slouken@302
   401
					}
slouken@302
   402
					/* Y axis */
slouken@302
   403
					if ((curstate & ((1<<JP_UP)|(1<<JP_DOWN))) != (prevstate & ((1<<JP_UP)|(1<<JP_DOWN)))) {
slouken@302
   404
						curaxis=0;
slouken@302
   405
						if (curstate & (1<<JP_UP)) {
slouken@302
   406
							curaxis=-128;
slouken@302
   407
						} else if (curstate & (1<<JP_DOWN)) {
slouken@302
   408
							curaxis=127;
slouken@302
   409
						}					
slouken@302
   410
						SDL_PrivateJoystickAxis(joystick,1,curaxis);
slouken@302
   411
					}
slouken@302
   412
					/* Buttons */
slouken@302
   413
					for (i=0;i<JP_NUM_BUTTONS;i++) {
slouken@302
   414
						int button;
slouken@302
   415
						
slouken@302
   416
						button=1<<jp_buttons[i];
slouken@302
   417
slouken@302
   418
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   419
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   420
						}
slouken@302
   421
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   422
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   423
						}
slouken@302
   424
					}
slouken@302
   425
				}
slouken@302
   426
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   427
			}
slouken@302
   428
			break;
slouken@302
   429
		case PORTA_JOY0:
slouken@302
   430
		case PORTA_JOY1:
slouken@302
   431
		case PORTB_JOY0:
slouken@302
   432
		case PORTB_JOY1:
slouken@302
   433
			{
slouken@302
   434
				int fire_shift=0,dir_shift=0;
slouken@302
   435
				
slouken@302
   436
				if (numjoystick==PORTA_JOY0) {	fire_shift=0; dir_shift=0; }
slouken@302
   437
				if (numjoystick==PORTA_JOY1) {	fire_shift=1; dir_shift=4; }
slouken@302
   438
				if (numjoystick==PORTB_JOY0) {	fire_shift=2; dir_shift=8; }
slouken@302
   439
				if (numjoystick==PORTB_JOY1) {	fire_shift=3; dir_shift=12; }
slouken@302
   440
slouken@302
   441
				curstate = (jp_directions>>dir_shift) & 15;
slouken@302
   442
				curstate |= ((jp_fires>>fire_shift) & 1)<<4;
slouken@302
   443
slouken@302
   444
				if (curstate != prevstate) {
slouken@302
   445
					/* X axis */
slouken@302
   446
					if ((curstate & (PORT_JS_LEFT|PORT_JS_RIGHT)) != (prevstate & (PORT_JS_LEFT|PORT_JS_RIGHT))) {
slouken@302
   447
						curaxis=0;
slouken@302
   448
						if (curstate & PORT_JS_LEFT) {
slouken@302
   449
							curaxis=-128;
slouken@302
   450
						} else if (curstate & PORT_JS_RIGHT) {
slouken@302
   451
							curaxis=127;
slouken@302
   452
						}					
slouken@302
   453
						SDL_PrivateJoystickAxis(joystick,0,curaxis);
slouken@302
   454
					}
slouken@302
   455
					/* Y axis */
slouken@302
   456
					if ((curstate & (PORT_JS_UP|PORT_JS_DOWN)) != (prevstate & (PORT_JS_UP|PORT_JS_DOWN))) {
slouken@302
   457
						curaxis=0;
slouken@302
   458
						if (curstate & PORT_JS_UP) {
slouken@302
   459
							curaxis=-128;
slouken@302
   460
						} else if (curstate & PORT_JS_DOWN) {
slouken@302
   461
							curaxis=127;
slouken@302
   462
						}					
slouken@302
   463
						SDL_PrivateJoystickAxis(joystick,1,curaxis);
slouken@302
   464
					}
slouken@302
   465
					/* Button */
slouken@302
   466
					if ((curstate & PORT_JS_FIRE) && !(prevstate & PORT_JS_FIRE)) {
slouken@302
   467
						SDL_PrivateJoystickButton(joystick,0,SDL_PRESSED);
slouken@302
   468
					}
slouken@302
   469
					if (!(curstate & PORT_JS_FIRE) && (prevstate & PORT_JS_FIRE)) {
slouken@302
   470
						SDL_PrivateJoystickButton(joystick,0,SDL_RELEASED);
slouken@302
   471
					}
slouken@302
   472
				}
slouken@302
   473
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   474
			}
slouken@302
   475
			break;
slouken@302
   476
		case PORTA_LP:
slouken@302
   477
			{
slouken@302
   478
				int i;
slouken@302
   479
slouken@302
   480
				curstate = jp_lightpens[0]>>1;
slouken@302
   481
				curstate |= (jp_lightpens[1]>>1)<<15;
slouken@302
   482
				curstate |= (jp_fires & 3)<<30;
slouken@302
   483
slouken@302
   484
				if (curstate != prevstate) {
slouken@302
   485
					/* X axis */
slouken@302
   486
					SDL_PrivateJoystickAxis(joystick,0,(jp_lightpens[0]>>8)-128);
slouken@302
   487
					/* Y axis */
slouken@302
   488
					SDL_PrivateJoystickAxis(joystick,1,(jp_lightpens[1]>>8)-128);
slouken@302
   489
					/* Buttons */
slouken@302
   490
					for (i=0;i<2;i++) {
slouken@302
   491
						int button;
slouken@302
   492
						
slouken@302
   493
						button=1<<(30+i);
slouken@302
   494
slouken@302
   495
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   496
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   497
						}
slouken@302
   498
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   499
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   500
						}
slouken@302
   501
					}
slouken@302
   502
				}
slouken@302
   503
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   504
			}
slouken@302
   505
			break;
slouken@302
   506
		case PORTA_ANPAD:
slouken@302
   507
		case PORTB_ANPAD:
slouken@302
   508
			{
slouken@302
   509
				int numpaddle, i;
slouken@302
   510
				
slouken@302
   511
				numpaddle=0<<1;
slouken@302
   512
				if (numjoystick==PORTB_ANPAD) numpaddle=1<<1;
slouken@302
   513
slouken@302
   514
				curstate = jp_paddles[numpaddle]>>1;
slouken@302
   515
				curstate |= (jp_paddles[numpaddle+1]>>1)<<15;
slouken@302
   516
				curstate |= ((jp_fires>>numpaddle) & 3)<<30;
slouken@302
   517
slouken@302
   518
				if (curstate != prevstate) {
slouken@302
   519
					/* X axis */
slouken@302
   520
					SDL_PrivateJoystickAxis(joystick,0,(jp_paddles[numpaddle]>>8)-128);
slouken@302
   521
					/* Y axis */
slouken@302
   522
					SDL_PrivateJoystickAxis(joystick,1,(jp_paddles[numpaddle+1]>>8)-128);
slouken@302
   523
					/* Buttons */
slouken@302
   524
					for (i=0;i<2;i++) {
slouken@302
   525
						int button;
slouken@302
   526
						
slouken@302
   527
						button=1<<(30+i);
slouken@302
   528
slouken@302
   529
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   530
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   531
						}
slouken@302
   532
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   533
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   534
						}
slouken@302
   535
					}
slouken@302
   536
				}
slouken@302
   537
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   538
			}
slouken@302
   539
			break;
slouken@302
   540
#if 0
slouken@302
   541
		case PARA_JOY0:
slouken@302
   542
		case PARA_JOY1:
slouken@302
   543
			break;
slouken@302
   544
#endif
slouken@302
   545
	};
slouken@302
   546
slouken@302
   547
	return;
slouken@302
   548
}
slouken@302
   549
slouken@302
   550
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@302
   551
{
slouken@302
   552
	return;
slouken@302
   553
}
slouken@302
   554
slouken@302
   555
void SDL_SYS_JoystickQuit(void)
slouken@302
   556
{
slouken@302
   557
	SDL_numjoysticks=0;
slouken@302
   558
	return;
slouken@302
   559
}
slouken@302
   560
slouken@302
   561
/*--- Joypad I/O read/write interface ---*/
slouken@302
   562
slouken@302
   563
#define JOYPAD_IO_BASE (0xffff9200)
slouken@302
   564
struct JOYPAD_IO_S {
slouken@302
   565
	Uint16 fires;
slouken@302
   566
	Uint16 directions;
slouken@302
   567
	Uint16 dummy1[6];
slouken@302
   568
	Uint16 paddles[4];
slouken@302
   569
	Uint16 dummy2[4];
slouken@302
   570
	Uint16 lightpens[2];
slouken@302
   571
};
slouken@302
   572
#define JOYPAD_IO ((*(volatile struct JOYPAD_IO_S *)JOYPAD_IO_BASE))
slouken@302
   573
slouken@302
   574
static void UpdateJoypads(void)
slouken@302
   575
{
slouken@302
   576
	Uint16 tmp;
slouken@302
   577
slouken@302
   578
	/*--- This function is called in supervisor mode ---*/
slouken@302
   579
slouken@302
   580
	/* Update joysticks */
slouken@302
   581
	jp_fires = (~(JOYPAD_IO.fires)) & 15;
slouken@302
   582
	jp_directions = (~(JOYPAD_IO.directions));
slouken@302
   583
	
slouken@302
   584
	/* Update lightpen */
slouken@302
   585
	tmp = JOYPAD_IO.lightpens[0] & 1023;
slouken@302
   586
	jp_lightpens[0] = (tmp<<6) | (tmp>>4);
slouken@302
   587
	tmp = JOYPAD_IO.lightpens[1] & 1023;
slouken@302
   588
	jp_lightpens[1] = (tmp<<6) | (tmp>>4);
slouken@302
   589
	
slouken@302
   590
	/* Update paddles */
slouken@302
   591
	tmp = (JOYPAD_IO.paddles[0] & 255);
slouken@302
   592
	jp_paddles[0] = (tmp<<8) | tmp;
slouken@302
   593
	tmp = (JOYPAD_IO.paddles[1] & 255);
slouken@302
   594
	jp_paddles[1] = (tmp<<8) | tmp;
slouken@302
   595
	tmp = (JOYPAD_IO.paddles[2] & 255);
slouken@302
   596
	jp_paddles[2] = (tmp<<8) | tmp;
slouken@302
   597
	tmp = (JOYPAD_IO.paddles[3] & 255);
slouken@302
   598
	jp_paddles[3] = (tmp<<8) | tmp;
slouken@302
   599
slouken@302
   600
	/* Update joypad 0 */	
slouken@302
   601
	JOYPAD_IO.directions=0xfffe;
slouken@302
   602
	jp_joypads[0]=((~(JOYPAD_IO.fires)) & 3)<<(16);
slouken@302
   603
	JOYPAD_IO.directions=0xfffe;
slouken@302
   604
	jp_joypads[0] |= ((~(JOYPAD_IO.directions))>>8) & 15;
slouken@302
   605
slouken@302
   606
	JOYPAD_IO.directions=0xfffd;
slouken@302
   607
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+2);
slouken@302
   608
	JOYPAD_IO.directions=0xfffd;
slouken@302
   609
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<4;
slouken@302
   610
slouken@302
   611
	JOYPAD_IO.directions=0xfffb;
slouken@302
   612
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+4);
slouken@302
   613
	JOYPAD_IO.directions=0xfffb;
slouken@302
   614
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<8;
slouken@302
   615
slouken@302
   616
	JOYPAD_IO.directions=0xfff7;
slouken@302
   617
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+6);
slouken@302
   618
	JOYPAD_IO.directions=0xfff7;
slouken@302
   619
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<12;
slouken@302
   620
slouken@302
   621
	/* Update joypad 1 */	
slouken@302
   622
	JOYPAD_IO.directions=0xffef;
slouken@302
   623
	jp_joypads[1]=((~(JOYPAD_IO.fires)) & (3<<2))<<(16-2);
slouken@302
   624
	JOYPAD_IO.directions=0xffef;
slouken@302
   625
	jp_joypads[1] |= ((~(JOYPAD_IO.directions))>>12) & 15;
slouken@302
   626
slouken@302
   627
	JOYPAD_IO.directions=0xffdf;
slouken@302
   628
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16);
slouken@302
   629
	JOYPAD_IO.directions=0xffdf;
slouken@302
   630
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<4;
slouken@302
   631
slouken@302
   632
	JOYPAD_IO.directions=0xffbf;
slouken@302
   633
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16+2);
slouken@302
   634
	JOYPAD_IO.directions=0xffbf;
slouken@302
   635
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<8;
slouken@302
   636
slouken@302
   637
	JOYPAD_IO.directions=0xff7f;
slouken@302
   638
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16+4);
slouken@302
   639
	JOYPAD_IO.directions=0xff7f;
slouken@302
   640
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<12;
slouken@302
   641
}