-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontrollers.c
More file actions
163 lines (129 loc) · 3.74 KB
/
controllers.c
File metadata and controls
163 lines (129 loc) · 3.74 KB
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
#include <psx.h>
#include <string.h>
#include "include/controllers.h"
void InitPad(){
PADSIO_CTRL(0) = 0x40;
PADSIO_BAUD(0) = 0x88;
PADSIO_MODE(0) = 13;
PADSIO_CTRL(0) = 0;
BusyLoop(10);
PADSIO_CTRL(0) = 2;
BusyLoop(10);
PADSIO_CTRL(0) = 0x2002;
BusyLoop(10);
PADSIO_CTRL(0) = 0;
}
void BusyLoop(int count){
volatile int cycles = count;
while (cycles--);
}
void ResetPad(Controller* ctrl)
{
/*Clear controller data*/
memset(ctrl, 0, sizeof(Controller));
/*Treat controller as disconnected*/
ctrl->Type = PAD_NONE;
/*Reset cursor to center of the screen*/
ctrl->CursorX = 160;
ctrl->CursorY = 120;
}
void SendData(int pad_n, unsigned char *in, unsigned char *out, int len)
{
if (!in || !out)
return;
/*This is how the BIOS does it*/
uint16_t mask = pad_n == 0 ? 0x0000 : 0x2000;
PADSIO_CTRL(0) = mask | 2;
PADSIO_DATA(0);
BusyLoop(40);
PADSIO_CTRL(0) = mask | 0x1003;
while (!(PADSIO_STATUS(0) & 1));
for(int x = 0; x < len; x++)
{
/*Wait for TX ready*/
while((PADSIO_STATUS(0) & 4) < 1);
PADSIO_DATA(0) = *in;
in++;
BusyLoop(25);
/*Read RX status flag*/
while((PADSIO_STATUS(0) & 2) < 1);
/*Busy loop only after initial byte*/
if(x == 0) BusyLoop(40);
*out = PADSIO_DATA(0);
out++;
}
PADSIO_CTRL(0) = 0;
}
void ReadPad(Controller* ctrl, int pad_n)
{
unsigned char DataToSend[] = {1, 0x42, 0, 0, 0, 0, 0, 0, 0}; /*Standard data polling command*/
unsigned char ReceivedData[16];
unsigned char ConfigStart[] = {1, 0x43, 0, 1, 0}; /*Config entry command*/
unsigned char ConfigStop[] = {1, 0x43, 0, 0, 0, 0, 0, 0, 0}; /*Config exit command*/
unsigned char ConfigAnalog[] = {1, 0x44, 0, 1, 3, 0, 0, 0, 0}; /*Permanent analog on command*/
unsigned char ConfigRumble[] = {1, 0x4D, 0, 0, 1, 255, 255, 255, 255}; /*Enable rumble motors*/
/*Remove rumble values*/
DataToSend[3] = 0;
DataToSend[4] = 0;
/*Clear receive buffer*/
memset(&ReceivedData, 0, sizeof(ReceivedData));
switch(ctrl->ConfigState)
{
default:
DataToSend[3] = ctrl->SmallMotor;
DataToSend[4] = ctrl->BigMotor;
/*Read button status*/
SendData(pad_n, DataToSend, ReceivedData, sizeof(DataToSend));
/*Check if anything is connected (line not floating high)*/
if(ReceivedData[1] == PAD_NONE)
{
ResetPad(ctrl);
}
else
{
/*Check if controller type changed from previous reading*/
if(ctrl->Type != ReceivedData[1]) ctrl->ConfigState = 0;
/*Store type*/
ctrl->Type = ReceivedData[1];
/*Get digital buttons*/
ctrl->Buttons = ~((ReceivedData[3] << 8) | ReceivedData[4]);
/*Check if this is analog controller*/
if(ctrl->Type == PAD_ANALOG)
{
/*Get analog sticks*/
ctrl->LeftStickX = ReceivedData[7] - 128;
ctrl->LeftStickY = ReceivedData[8] - 128;
ctrl->RightStickX = ReceivedData[5] - 128;
ctrl->RightStickY = ReceivedData[6] - 128;
}
/*Check if this is a mouse*/
if(ctrl->Type == PAD_MOUSE){
ctrl->CursorX += (char)ReceivedData[5];
ctrl->CursorY += (char)ReceivedData[6];
/*Clipping*/
if(ctrl->CursorX < 0) ctrl->CursorX = 0;
if(ctrl->CursorY < 0) ctrl->CursorY = 0;
if(ctrl->CursorX > 320) ctrl->CursorX = 320;
if(ctrl->CursorY > 240) ctrl->CursorY = 240;
}
}
break;
case 1:
/*Enter configuration mode*/
SendData(pad_n, ConfigStart, ReceivedData, sizeof(ConfigStart));
break;
case 2:
/*Set auto analog mode*/
SendData(pad_n, ConfigAnalog, ReceivedData, sizeof(ConfigAnalog));
break;
case 3:
/*Configure rumble*/
SendData(pad_n, ConfigRumble, ReceivedData, sizeof(ConfigRumble));
break;
case 4:
/*Exit configuration mode*/
SendData(pad_n, ConfigStop, ReceivedData, sizeof(ConfigStop));
break;
}
if(ctrl->ConfigState < 6) ctrl->ConfigState++;
}