-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjoystickinput.c
600 lines (522 loc) · 17.3 KB
/
joystickinput.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include "defines.h"
#include "joystickinput.h"
#include "logger.h"
// JoyStick configuration
// These were renamed from Left and Right to preserve maintainer sanity
// JS.UseMouse: 0=keyboard,1=mouse,2=audio,3=joystick
// JS.HiRes: 0=lowres,1=software,2=tandy,3=ccmax
JoyStick LeftJS;
JoyStick RightJS;
/*
/ Coco joysticks had restricted resolution due to use of 6 bit DAC compare
/ But a 640x225 screen resolution is possible for CoCo3 screen.
/ As a result many programs desire a higher resolution pointing device
/
/ Higher than 6 bit resolution can be obtained using 6 bit DAC with
/ special code that rapidly compares joystick values to the DAC voltage
/ as it changes also CocoMax cart used a ADC that retuned an 8 bit value.
/
/ Windows HID joysticks and mice have at least 16 bit resolution. So
/ code has been added to support higher resolution using one of the
/ two above methods.
/
/ To allow for increased resolution stick values are stored in
/ fourteen bits. This means the second byte contains a 0-63
/ value which makes initial DAC comparisons easier.
*/
// for DoubleSpeedFlag
extern SystemState EmuState;
// Clock cycles since Joystick ramp started
extern int JS_Ramp_Clock=0;
// Software high resolution disabled
// DAC change is used for software high resolution joystick.
// It is used to simulate the normal DAC comparator time delay.
// static int DAC_Change;
// The following rising and falling values per MPU cycle were scaled
// from "deep scan" figure in "HI-RES INTERFACE" by Kowalski, Gault,
// and Marentes. Unfortunatly the magic program to make software high
// resolution work uses the one cycle timing difference between LDB ,X
// (4 cycles) and LDB, $FF00 (5 cycles) to get 10x resolution. Because
// of the way the MPU is emulated Vcc does not know the addressing mode
// at the time of the memory access and DAC compare. It therefore can
// only supply 5 meaningful samples instead of 10. As a result the best
// Vcc can do is 5x resolution.
//static int DAC_Rising[10] ={256,256,256,256, 48, 32, 24, 20, 0, 0};
//static int DAC_Falling[10]={256,256,256,256,224,116, 64, 56, 48, 0};
// static int DAC_Rising[10] ={256,256,256,256, 40, 40, 22, 22, 0, 0};
// static int DAC_Falling[10]={256,256,256,256,160,160, 60, 60, 24, 24};
// Hires ramp flag
static int JS_Ramp_On;
// Hires ramp constants. Determined during testing
#define TANDYRAMPMIN 1200
#define TANDYRAMPMAX 10950
#define TANDYRAMPMUL 37
#define CCMAXRAMPMIN 800
#define CCMAXRAMPMAX 14000
#define CCMAXRAMPMUL 21
static int sticktarg = 0; // Target stick cycle count
// Joystick values (0-16383)
#define STICKMAX 16383
#define STICKMID 8191
unsigned int LeftStickX = STICKMID;
unsigned int LeftStickY = STICKMID;
unsigned int RightStickX = STICKMID;
unsigned int RightStickY = STICKMID;
static unsigned char LeftStickNumber = 0;
static unsigned char RightStickNumber = 0;
// Button states
static unsigned char LeftButton1Status = 0;
static unsigned char RightButton1Status = 0;
static unsigned char LeftButton2Status = 0;
static unsigned char RightButton2Status = 0;
// FIXME Direct input not working for ARM - disable joysticks for arm builds
#ifdef _M_ARM
unsigned int Joysticks[MAXSTICKS] = {NULL};
#else
static LPDIRECTINPUTDEVICE8 Joysticks[MAXSTICKS];
#endif
char StickName[MAXSTICKS][STRLEN];
static unsigned char JoyStickIndex=0;
#ifdef _M_ARM
#else
static LPDIRECTINPUT8 di;
BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* , VOID* );
BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* , VOID* );
#endif
static unsigned char CurrentStick;
unsigned int get_pot_value(unsigned char);
inline int vccJoystickType();
/*****************************************************************************/
// Locate connected joysticks. Called by config.c
int EnumerateJoysticks(void)
{
#ifdef _M_ARM
return(0);
#else
HRESULT hr;
JoyStickIndex=0;
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL),
DIRECTINPUT_VERSION,IID_IDirectInput8,(VOID**)&di,NULL)))
return(0);
if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL,
enumCallback,NULL,DIEDFL_ATTACHEDONLY)))
return(0);
return(JoyStickIndex);
#endif
}
/*****************************************************************************/
#ifndef _M_ARM
BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;
hr = di->CreateDevice(instance->guidInstance, &Joysticks[JoyStickIndex],NULL);
strncpy(StickName[JoyStickIndex],instance->tszProductName,STRLEN);
JoyStickIndex++;
return(JoyStickIndex<MAXSTICKS);
}
#endif
/*****************************************************************************/
// Init joystick called by config.c
bool InitJoyStick (unsigned char StickNumber)
{
#ifndef _M_ARM
// DIDEVCAPS capabilities;
HRESULT hr;
CurrentStick=StickNumber;
if (Joysticks[StickNumber]==NULL)
return(0);
if (FAILED(hr= Joysticks[StickNumber]->SetDataFormat(&c_dfDIJoystick2)))
return(0);
// if (FAILED(hr= Joysticks[StickNumber]->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE )))
// return(0);
//Fails for some reason Investigate this
// if (FAILED(hr= Joysticks[StickNumber]->GetCapabilities(&capabilities)))
// return(0);
if (FAILED(hr= Joysticks[StickNumber]->EnumObjects(enumAxesCallback,NULL,DIDFT_AXIS)))
return(0);
return(1); //return true on success
#else
return(1);
#endif
}
/*****************************************************************************/
#ifndef _M_ARM
BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg= (HWND)context;
DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
propRange.lMin=0;
propRange.lMax=0xFFFF;
if (FAILED(Joysticks[CurrentStick]->SetProperty(DIPROP_RANGE, &propRange.diph)))
return(DIENUM_STOP);
return(DIENUM_CONTINUE);
}
#endif
/*****************************************************************************/
// Called by get_pot_value to read data from physical joystick
#ifndef _M_ARM
HRESULT
JoyStickPoll(DIJOYSTATE2 *js,unsigned char StickNumber)
{
HRESULT hr;
if (Joysticks[StickNumber] ==NULL)
return (S_OK);
hr=Joysticks[StickNumber]->Poll();
if (FAILED(hr)) {
hr=Joysticks[StickNumber]->Acquire();
while (hr == DIERR_INPUTLOST)
hr = Joysticks[StickNumber]->Acquire();
if (hr == DIERR_INVALIDPARAM) //|| (hr == DIERR_NOTINITALIZED
return(E_FAIL);
if (hr == DIERR_OTHERAPPHASPRIO)
return(S_OK);
}
if (FAILED(hr= Joysticks[StickNumber]->GetDeviceState(sizeof(DIJOYSTATE2), js)))
return(hr);
return(S_OK);
}
#endif
/*****************************************************************************/
// inline function returns joystick emulation type
inline int vccJoystickType() {
return (GetMuxState() & 2) ? LeftJS.HiRes : RightJS.HiRes;
}
/*****************************************************************************/
// Called by mc6821 when $FF20 is written
void
vccJoystickStartTandy(unsigned char data, unsigned char next)
{
// FIXME: Disabled software joystick. Instruction timing too poor.
// switch(vccJoystickType()) {
// case 1: // Software
// DAC_Change = (next>>2)-(data>>2); // For software hires
// JS_Ramp_Clock = 0;
// break;
// case 2: // Tandy
if (vccJoystickType() == 2) {
if ( next == 2 ) {
JS_Ramp_On = 1;
sticktarg = 0;
JS_Ramp_Clock = 0;
}
// break;
}
}
/*****************************************************************************/
// Called by mc6821 when zero is written to $FF00
void
vccJoystickStartCCMax()
{
if (vccJoystickType() != 3) return;
JS_Ramp_On = 1;
sticktarg = 0;
JS_Ramp_Clock = 0;
}
/*****************************************************************************/
// Called by keyboard.c when FF00 is read to add joystick bits to scancode
unsigned char
vccJoystickGetScan(unsigned char code)
{
unsigned int StickValue;
unsigned char axis = GetMuxState(); // 0 rx, 1 ry, 2 lx, 3 ly
int JS_Type = vccJoystickType();
StickValue = get_pot_value(axis);
// If hires hardware joystick
if (JS_Type > 1) {
if (JS_Ramp_On) {
// if target is zero set it based on current stick value
if (sticktarg == 0) {
if (JS_Type == 2) {
sticktarg = TANDYRAMPMIN + ((StickValue*TANDYRAMPMUL)>>6);
if(sticktarg>TANDYRAMPMAX) sticktarg=TANDYRAMPMAX;
// cut target in half if double speed
if (!EmuState.DoubleSpeedFlag) sticktarg = sticktarg/2;
} else if (JS_Type == 3) { //ccmax
sticktarg = CCMAXRAMPMIN + ((StickValue*CCMAXRAMPMUL)>>6);
if(sticktarg>CCMAXRAMPMAX) sticktarg=CCMAXRAMPMAX;
}
}
}
// If clock exceeds target set compare bit and stop ramp
if (JS_Ramp_Clock > sticktarg) {
code |= 0x80;
JS_Ramp_On = 0;
}
// else standard or software hires
} else if (StickValue != 0) { // OS9 joyin needs this for koronis rift
unsigned int val = DACState();
// Disabled software joystick
// if ((JS_Type==1) && (JS_Ramp_Clock < 10)) { // software hires?
// switch (DAC_Change) {
// case 1:
// val -= DAC_Rising[JS_Ramp_Clock];
// break;
// case -1:
// val += DAC_Falling[JS_Ramp_Clock];
// break;
// }
// }
if (StickValue >= val) {
code |= 0x80;
}
}
if (LeftButton1Status == 1) {
//Left Joystick Button 1 Down?
code = code & 0xFD;
}
if (RightButton1Status == 1) {
//Right Joystick Button 1 Down?
code = code & 0xFE;
}
if (LeftButton2Status == 1) {
//Left Joystick Button 2 Down?
code = code & 0xF7;
}
if (RightButton2Status == 1) {
//Right Joystick Button 2 Down?
code = code & 0xFB;
}
return code;
}
/*****************************************************************************/
// Called by vcc.c on mouse moves. x and y range 0-3fff
void joystick(unsigned int x,unsigned int y)
{
if (x>STICKMAX) x=STICKMAX;
if (y>STICKMAX) y=STICKMAX;
if (LeftJS.UseMouse==1) {
LeftStickX=x;
LeftStickY=y;
}
if (RightJS.UseMouse==1) {
RightStickX=x;
RightStickY=y;
}
return;
}
/*****************************************************************************/
void
SetStickNumbers(unsigned char Temp1,unsigned char Temp2)
{
LeftStickNumber=Temp1;
RightStickNumber=Temp2;
return;
}
/*****************************************************************************/
// called by pia0_read->vccKeyboardGetScan->vccJoystickGetScan
unsigned int
get_pot_value(unsigned char pot)
{
#ifndef _M_ARM
DIJOYSTATE2 Stick1;
// Poll left joystick if attached
if (LeftJS.UseMouse==3) {
JoyStickPoll(&Stick1,LeftStickNumber);
LeftStickX = (unsigned short)Stick1.lX>>2; // Not tested
LeftStickY = (unsigned short)Stick1.lY>>2; // Not tested
LeftButton1Status= Stick1.rgbButtons[0]>>7;
LeftButton2Status= Stick1.rgbButtons[1]>>7;
}
// Poll right joystick if attached
if (RightJS.UseMouse ==3) {
JoyStickPoll(&Stick1,RightStickNumber);
RightStickX = (unsigned short)Stick1.lX>>2; // Not tested
RightStickY = (unsigned short)Stick1.lY>>2; // Not tested
RightButton1Status= Stick1.rgbButtons[0]>>7;
RightButton2Status= Stick1.rgbButtons[1]>>7;
}
#endif
switch (pot) {
case 0:
return(RightStickX);
break;
case 1:
return(RightStickY);
break;
case 2:
return(LeftStickX);
break;
case 3:
return(LeftStickY);
break;
}
return (0);
}
/*****************************************************************************/
// Called by keyboard.h to handle keyboard joysticks
unsigned char
SetMouseStatus(unsigned char ScanCode,unsigned char Phase)
{
unsigned char ReturnValue=ScanCode;
// Mask scan code high bit to accept extended keys.
unsigned char TempCode = ScanCode & 0x7F;
switch (Phase) {
case 0:
if (LeftJS.UseMouse==0) {
if (TempCode==LeftJS.Left) {
LeftStickX=STICKMID;
ReturnValue=0;
}
if (TempCode==LeftJS.Right) {
LeftStickX=STICKMID;
ReturnValue=0;
}
if (TempCode==LeftJS.Up) {
LeftStickY=STICKMID;
ReturnValue=0;
}
if (TempCode==LeftJS.Down) {
LeftStickY=STICKMID;
ReturnValue=0;
}
if (TempCode==LeftJS.Fire1) {
LeftButton1Status=0;
ReturnValue=0;
}
if (TempCode==LeftJS.Fire2) {
LeftButton2Status=0;
ReturnValue=0;
}
}
if (RightJS.UseMouse==0) {
if (TempCode==RightJS.Left) {
RightStickX=STICKMID;
ReturnValue=0;
}
if (TempCode==RightJS.Right) {
RightStickX=STICKMID;
ReturnValue=0;
}
if (TempCode==RightJS.Up) {
RightStickY=STICKMID;
ReturnValue=0;
}
if (TempCode==RightJS.Down) {
RightStickY=STICKMID;
ReturnValue=0;
}
if (TempCode==RightJS.Fire1) {
RightButton1Status=0;
ReturnValue=0;
}
if (TempCode==RightJS.Fire2) {
RightButton2Status=0;
ReturnValue=0;
}
}
break;
case 1:
if (LeftJS.UseMouse==0) {
if (TempCode==LeftJS.Left) {
LeftStickX=0;
ReturnValue=0;
}
if (TempCode==LeftJS.Right) {
LeftStickX=STICKMAX;
ReturnValue=0;
}
if (TempCode==LeftJS.Up) {
LeftStickY=0;
ReturnValue=0;
}
if (TempCode==LeftJS.Down) {
LeftStickY=STICKMAX;
ReturnValue=0;
}
if (TempCode==LeftJS.Fire1) {
LeftButton1Status=1;
ReturnValue=0;
}
if (TempCode==LeftJS.Fire2) {
LeftButton2Status=1;
ReturnValue=0;
}
}
if (RightJS.UseMouse==0) {
if (TempCode==RightJS.Left) {
ReturnValue=0;
RightStickX=0;
}
if (TempCode==RightJS.Right) {
RightStickX=STICKMAX;
ReturnValue=0;
}
if (TempCode==RightJS.Up) {
RightStickY=0;
ReturnValue=0;
}
if (TempCode==RightJS.Down) {
RightStickY=STICKMAX;
ReturnValue=0;
}
if (TempCode==RightJS.Fire1) {
RightButton1Status=1;
ReturnValue=0;
}
if (TempCode==RightJS.Fire2) {
RightButton2Status=1;
ReturnValue=0;
}
}
break;
}
return(ReturnValue);
}
/****************************************************************************/
// Called by vcc.c to set mouse joystick buttons
// Side=0 Left Button Side=1 Right Button State 1=Down
void
SetButtonStatus(unsigned char Side,unsigned char State)
{
unsigned char Btemp;
Btemp = (Side<<1) | State;
if (LeftJS.UseMouse==1)
switch (Btemp) {
case 0:
LeftButton1Status=0;
break;
case 1:
LeftButton1Status=1;
break;
case 2:
LeftButton2Status=0;
break;
case 3:
LeftButton2Status=1;
break;
}
if (RightJS.UseMouse==1)
switch (Btemp) {
case 0:
RightButton1Status=0;
break;
case 1:
RightButton1Status=1;
break;
case 2:
RightButton2Status=0;
break;
case 3:
RightButton2Status=1;
break;
}
}