/
XIO.cpp
563 lines (509 loc) · 10.2 KB
/
XIO.cpp
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
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "XIO.h"
// Added to the brake whenever the brake is depressed.
#define HARD_BRAKE_MULTIPLIER 128
#define SOFT_BRAKE_MULTIPLIER 64
/*
Globals
*/
XIO_API unsigned char g_bKeyImm = 63; // Last input state of buttons or maybe from game?
XIO_API unsigned char g_bKeyExc; // Current input state of buttons to be sent back to game.
XIO_API unsigned char g_bBrake; // Guessing the Brake state.
XIO_API unsigned char g_bHandle = 128; // Guessing the Handlebar state.
XIO_API unsigned char g_bAccelerator; // Accelerator State?
XIO_API unsigned char g_bLAccel; // Left Side Accelerator?
XIO_API unsigned char g_bLAccelDif; // Last state of Left Accelerator?
XIO_API unsigned char g_bLAccelEnd; // no idea...
XIO_API unsigned char g_bRAccel; // Right Side Accelerator?
XIO_API unsigned char g_bRAccelDif; // Last state of Right Accelerator?
XIO_API unsigned char g_bRAccelEnd; // no idea...
XIO_API unsigned char lastLeftPedalVelocity; // no idea...
XIO_API unsigned char lastRightPedalVelocity; // no idea...
XIO_API unsigned char g_bLAccelDifLast; // no idea...
XIO_API unsigned char g_bRAccelDifLast; // no idea...
XIO_API unsigned char leftPedalVelocity = 0;
XIO_API unsigned char rightPedalVelocity = 0;
/*
Simple Constructor for XIO Object.
*/
XIO::XIO() {
m3 = 3;
m4 = 3;
pedalData = 0;
}
/*
Destructor for XIO Object.
*/
XIO::~XIO() {
closeInterface();
}
/*
Name: XIO::openInterface
Purpose: Sets up a new interface to the hardware.
Input: None
Output: int result
*/
int XIO::openInterface() {
initialize();
return 1;
}
/*
Name: XIO::closeInterface
Purpose: Resets hardware.
Input: None
Output: None
*/
void XIO::closeInterface() {
initLamp();
setVibration(3, 1);
resetPedal();
}
/*
Name: XIO::initialize
Purpose: Initializes the IO Device.
Inputs: None
Outputs: None
*/
void XIO::initialize() {
initLamp();
initSteering();
initPedal();
initVibration();
}
/*
Name:XIO::initLamp
Purpose: Initializes the lamp state.
Input:Nothing
Output: Nothing
*/
void XIO::initLamp() {
lampCoinIOData = 0xD0;
lamp1 = 0xFF;
lamp2 = 0xFF;
}
/*
Name:XIO::initVibration
Purpose: Initializes the Vibration State.
Input: Nothing
Output: Nothing
*/
void XIO::initVibration() {
vibrationData = 3;
}
/*
Name:XIO::initSteering
Purpose: Initializes the Steering State.
Input: Nothing
Output: Nothing
*/
void XIO::initSteering() {
steeringData = 128;
}
/*
Name:XIO::initPedal
Purpose: Initializes the Pedal State.
Input: Nothing
Output: Nothing
*/
void XIO::initPedal() {
lastLeftPedalVelocity = 0;
lastRightPedalVelocity = 0;
g_bLAccelEnd = 0;
g_bRAccelEnd = 0;
g_bLAccelDif = 0;
g_bRAccelDif = 0;
g_bLAccelDifLast = 0;
g_bRAccelDifLast = 0;
g_bLAccel = 0;
g_bRAccel = 0;
resetPedal();
}
/*
Name:XIO::resetPedal
Purpose: Resets the Pedal State.
Input: Nothing
Output: Nothing
*/
void XIO::resetPedal() {
pedalData |= 0x22;
pedalData &= 0xEE;
pedalData |= 0x11;
pedalData &= 0xDD;
}
/*
Name:XIO::selectLamp
Purpose: Sets lamp state for the select button?
Input: lampState (uchar)
Output: Nothing
*/
void XIO::selectLamp(int lampState) {
unsigned char lampDataNew;
if (lampState) {
lampDataNew = lampCoinIOData & 0xEF;
}
else {
lampDataNew = lampCoinIOData | 0x10;
}
lampCoinIOData = lampDataNew;
}
/*
Name:XIO::startLamp
Purpose: Sets lamp state for the start button?
Input: lampState (uchar)
Output: Nothing
*/
void XIO::startLamp(int lampState) {
unsigned char lampDataNew;
if (lampState) {
lampDataNew = lampCoinIOData | 8;
}
else {
lampDataNew = lampCoinIOData & 0xF7;
}
lampCoinIOData = lampDataNew;
}
/*
Name:XIO::leftLamp
Purpose: Sets lamp state for the left button?
Input: lampState (uchar)
Output: Nothing
*/
void XIO::leftLamp(unsigned char lampState) {
unsigned char lampDataNew;
if (lampState) {
lampDataNew = lampCoinIOData & 0xBF;
}
else {
lampDataNew = lampCoinIOData | 0x40;
}
lampCoinIOData = lampDataNew;
}
/*
Name:XIO::rightLamp
Purpose: Sets lamp state for the right button?
Input: lampState (uchar)
Output: Nothing
*/
void XIO::rightLamp(unsigned char lampState) {
unsigned char lampDataNew;
if (lampState) {
lampDataNew = lampCoinIOData & 0x7F;
}
else {
lampDataNew = lampCoinIOData | 0x80;
}
lampCoinIOData = lampDataNew;
}
/*
Name:XIO::lamp
Purpose: No Idea Yet
Input: lampState
Output: Nothing
*/
void XIO::lamp(unsigned char lampState) {
if (lampState & 1) {
lampCoinIOData |= 0xD8;
return;
}
if (lampState & 2) {
lampCoinIOData = lampCoinIOData & 0xE7 | 0xC0;
return;
}
if (lampState & 4) {
lampCoinIOData = lampCoinIOData & 0x27 | 0x90;
}
else {
if (lampState & 8) {
lampCoinIOData = lampCoinIOData & 0x27 | 0x50;
return;
}
lampCoinIOData = lampCoinIOData & 0xF7 | 0xD0;
}
}
/*
Scan Keyboard Input and set gvals.
*/
#define VK_1 0x31
#define VK_Z 0x5A
#define VK_X 0x58
static const unsigned char playerKeys[6] = { VK_F3,VK_F2,VK_1,VK_RETURN,VK_LEFT,VK_RIGHT };
void XIO::keyboardInput() {
unsigned char result = 0xFF;
//Button Logic
for (int i = 0; i < sizeof(playerKeys); i++) {
if (GetAsyncKeyState(playerKeys[i])) {
result ^= (1 << i);
}
}
g_bKeyExc = result;
// Steering Logic
result = steeringData;
if (GetAsyncKeyState(VK_LEFT)) {
if (result - 32 <= 0) {
result = 0;
}
else {
result -= 32;
}
}
if (GetAsyncKeyState(VK_RIGHT)) {
if (result + 32 >= 254) {
result = 254;
}
else {
result += 32;
}
}
if (!GetAsyncKeyState(VK_RIGHT) && !GetAsyncKeyState(VK_LEFT)) {
if (result <= 128) {
if (result + 32 >= 128) {
result = 128;
}
else {
result += 32;
}
}
else {
if (result - 32 <= 128) {
result = 128;
}
else {
result -= 32;
}
}
}
steeringData = result;
// Accelerator Logic
if (GetAsyncKeyState(VK_X)) {
if (rightPedalVelocity >= 254) {
rightPedalVelocity = 254;
}
else {
rightPedalVelocity += 2;
}
}
else {
if (rightPedalVelocity <= 0) {
rightPedalVelocity = 0;
}
else {
rightPedalVelocity -= 2;
}
}
if (GetAsyncKeyState(VK_Z)) {
if (leftPedalVelocity >= 254) {
leftPedalVelocity = 254;
}
else {
leftPedalVelocity += 2;
}
}
else {
if (leftPedalVelocity <= 0) {
leftPedalVelocity = 0;
}
else {
leftPedalVelocity -= 2;
}
}
// Brake Logic
if (GetAsyncKeyState(VK_LCONTROL)) {
g_bBrake = 0x80;
}
if (GetAsyncKeyState(VK_SHIFT)) {
g_bBrake = 0xFF;
}
if (!GetAsyncKeyState(VK_LCONTROL) && !GetAsyncKeyState(VK_LSHIFT)) {
g_bBrake = 0;
}
}
/*
Name: XIO::update
Purpose: Used in an event loop to update internal IO states.
Input: Nothing
Output: Nothing
*/
void XIO::update() {
keyboardInput();
if (g_bKeyExc & 8) {
updateCoinCounter();
}
updateInputState();
updateHandleState();
updatePedalState();
}
/*
Name: XIO::updateCoinCounter
Purpose: Updates the coin counter state.
Input: Nothing
Output: Nothing
*/
void XIO::updateCoinCounter() {
int ctr = 4096;
do {
lampCoinIOData | 2;
--ctr;
} while (ctr);
lampCoinIOData &= 0xFD;
}
/*
Name: XIO::updateLampState
Purpose: Updates the lamp state.
Input: Nothing
Output: Nothing
*/
void XIO::updateLampState() {
unsigned char cLampState1 = lampCoinIOData;
if (g_bLAccelDif) {
if (cLampState1 & 0x40) {
leftLamp(1);
}
lamp1 = 63;
}
else if (!(cLampState1 & 0x40)) {
lamp1 -= 1;
if (!lamp1) {
leftLamp(0);
}
}
unsigned char cLampState2 = lampCoinIOData;
if (g_bRAccelDif) {
if (cLampState2 & 0x80) {
rightLamp(1);
}
lamp2 = 63;
}
else if (!(cLampState2 & 0x80)) {
lamp2 -= 1;
if (!lamp2) {
rightLamp(0);
}
}
}
/*
Name: XIO::updateInputState()
Purpose: Reads a byte from the I/O hardware, uses the previous state to calculate the new state, and stores both.
Inputs: None {global g_bKeyExc , g_bKeyImm}
Outputs: None
*/
void XIO::updateInputState() {
char deviceResponse;
unsigned char lastState;
unsigned char g_bKeyExcNew = 0;
// returns the byte representation of key states.
deviceResponse = ~g_bKeyExc;
if (deviceResponse & 1) {
g_bKeyExcNew = 4;
}
if (deviceResponse & 2) {
g_bKeyExcNew |= 2;
}
if (deviceResponse & 4) {
g_bKeyExcNew |= 8;
}
if (deviceResponse & 8) {
g_bKeyExcNew |= 1;
}
if (deviceResponse & 16) {
g_bKeyExcNew |= 16;
}
if (deviceResponse & 32) {
g_bKeyExcNew |= 32;
}
lastState = g_bKeyImm;
g_bKeyImm = g_bKeyExcNew;
g_bKeyExc = g_bKeyExcNew & (g_bKeyExcNew ^ lastState);
}
/*
Name: XIO::updateHandleState
Purpose: Updates the handle state.
Input: Nothing
Output: Nothing
*/
void XIO::updateHandleState() {
g_bHandle = steeringData;
}
/*
Name: XIO::updatePedalState
Purpose: Updates the pedal state.
Input: Nothing
Output: Nothing
*/
void XIO::updatePedalState() {
//pedalData &= 0xDD;
g_bLAccelDif = abs(leftPedalVelocity - lastLeftPedalVelocity);
g_bLAccelEnd = leftPedalVelocity;
lastLeftPedalVelocity = leftPedalVelocity;
unsigned char g_bLAccelDif_copy = g_bLAccelDif;
if (leftPedalVelocity >= 0xFF) {
XIO::resetPedal();
g_bLAccelDif = g_bLAccelDif_copy;
}
g_bLAccel = 0;
if (g_bLAccelDifLast < g_bLAccelDif) {
g_bLAccel = 1;
}
g_bLAccelDifLast = g_bLAccelDif;
g_bRAccelDif = abs(rightPedalVelocity - lastRightPedalVelocity) ;
g_bRAccelEnd = rightPedalVelocity;
unsigned char g_bRAccelDif_copy = g_bRAccelDif;
lastRightPedalVelocity = rightPedalVelocity;
if (rightPedalVelocity >= 0xFF) {
XIO::resetPedal();
g_bRAccelDif = g_bRAccelDif_copy;
}
g_bRAccel = 0;
if (g_bRAccelDifLast < g_bRAccelDif) {
g_bRAccel = 1;
}
g_bRAccelDifLast = g_bRAccelDif;
return;
}
/*
Name: XIO::setVibration
Purpose: Sets Vibration Level.
Input: char a2, int a3
Output: None
*/
void XIO::setVibration(unsigned char a2, int a3) {
int vibrationDataNew;
if (a3) {
vibrationDataNew = a2;
}
else {
vibrationDataNew = 3;
}
if (vibrationData != vibrationDataNew) {
vibrationData = vibrationDataNew;
}
}
/*
Name: XIO::setSteeringBar
Purpose: Sets the steering bar state.
Input: unsigned char inputData
Output: None
*/
void XIO::setSteeringBar(unsigned char inputData) {
//steeringData = inputData;
}
/*
Name: XIO::getSteeringBar
Purpose: Returns the steering bar state.
Input: None
Output: unsigned char SteeringState
*/
unsigned char XIO::getSteeringBar() {
return steeringData;
}
/*
Name: XIO::getVibration
Purpose: Returns the vibration state.
Input: None
Output: unsigned char vibrationState
*/
unsigned char XIO::getVibration() {
return vibrationData;
}