-
Notifications
You must be signed in to change notification settings - Fork 0
/
pspctrl.c
397 lines (353 loc) · 11.4 KB
/
pspctrl.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
#include <string.h>
#include <pspsdk.h>
#include <pspinit.h>
#include <pspctrl.h>
#include <pspctrl_kernel.h>
#include <pspsysevent.h>
#include <pspsystimer.h>
#include <pspsyscon.h>
PSP_MODULE_INFO("sceController_Service", 1007, 1, 10);
#define SLL_K1(k1) asm volatile("sll $k1, $k1, %0;" :: "r" (k1))
#define SET_K1(k1) asm volatile("move $k1, %0;" :: "r" (k1))
#define GET_K1(k1) asm volatile("move %0, $k1;" : "=r" (k1))
#define MOVE(a, b) asm volatile("move %0, %1;" : "=r" (a) : "r" (b))
#define INS(a, b, c, d) asm volatile("ins %0, %1, %2, %3;" : "=r" (a) : "r" (b, c, d))
typedef struct ButtonCallback {
unsigned int mask;
void (*cb)(int, int, void*);
unsigned int gp;
void *arg;
} ButtonCallback;
typedef struct sceSysconPacket
{
u8 unk00[4]; // +0x00 ?(0x00,0x00,0x00,0x00)
u8 unk04[2]; // +0x04 ?(arg2)
u8 status; // +0x06
u8 unk07; // +0x07 ?(0x00)
u8 unk08[4]; // +0x08 ?(0xff,0xff,0xff,0xff)
// transmit data
u8 tx_cmd; // +0x0C command code
u8 tx_len; // +0x0D number of transmit bytes
u8 tx_data[14]; // +0x0E transmit parameters
// receive data
u8 rx_sts; // +0x1C generic status
u8 rx_len; // +0x1D receive length
u8 rx_response; // +0x1E response code(tx_cmd or status code)
u8 rx_data[9]; // +0x1F receive parameters
// ?
u32 unk28; // +0x28
// user callback (when finish an access?)
void (*callback)(struct sceSysconPacket *,u32); // +0x2c
u32 callback_r28; // +0x30
u32 callback_arg2; // +0x34 arg2 of callback (arg4 of sceSycconCmdExec)
u8 unk38[0x0d]; // +0x38
u8 old_sts; // +0x45 old rx_sts
u8 cur_sts; // +0x46 current rx_sts
u8 unk47[0x21]; // +0x47
// data size == 0x60 ??
} sceSysconPacket;
PspSysEventHandler sysevent;//0x00002880
typedef struct SceCtrlOption {
SceSysTimerId timer; //0x000028C0
SceUID evid; //0x000028C4
int cycle; //0x000028C8
char sampling[2]; //0x000028CC
char g0x000028CE; //0x000028CE
char g0x000028CF; //0x000028CF
int g0x000028D0;//run ? //0x000028D0
//0x000028D4
char polling; //0x000028D5
short g0x000028D6; //0x000028D6
int g0x000028D8; //0x000028D8
sceSysconPacket packet; //0x000028DC
//0x0000293C
int g0x000029B8;
int g0x000029E0; //0x000029E0
int rapidfire[16]; //0x000029EC
//0x00002A02
char g0x00002B00[4]; //0x00002B00
char g0x00002B04; //0x00002B04
char g0x00002B05; //0x00002B05
char g0x00002B07; //0x00002B07
int g0x00002B58; //0x00002B58
int g0x00002B5C; //0x00002B5C
int g0x00002B64; //0x00002B64
int g0x00002B68; //0x00002B68
int g0x00002B6C; //0x00002B6C
int g0x00002B70; //0x00002B70
int g0x00002B74; //0x00002B74
int g0x00002B7C; //0x00002B7C
ButtonCallback callback[4]; //0x00002B80 - 0x00002BBF
int g0x00002BC0; //0x00002BC0
//0x00002BD4
} SceCtrlOption;
SceCtrlOption option;//0x000028C0
int module_start(SceSize args, void* argp) {
sceCtrlInit();
return 0;
}
int module_reboot_before(SceSize args, void *argp) {
sceCtrlEnd();
return 0;
}
int sceCtrlInit() {//not complete
memset(&option, 0, 0x314);
option.polling = 1;
option.g0x000029B8 = 0x00002BE0;
option.g0x00002B04 = -128;
option.g0x00002B05 = -128;
option.g0x000029E0 = 0x00002FE0;
option.g0x000028D8 = -1;
option.evid = sceKernelCreateEventFlag("SceCtrl", 1, 0, NULL);
option.timer = sceSTimerAlloc();
if(option.timer != NULL) {
SysTimerForKernel_CCC7A9E4(option.timer, 0x1, 0x30);
sceKernelRegisterSysEventHandler(&sysevent);
sceSyscon_driver_62012EAF(0);
int keyconf = sceKernelInitKeyConfig();
int var;
if(keyconf == 0x110) {
var = 0x0003FFFF;
} else if(keyconf == PSP_INIT_KEYCONFIG_VSH) {
var = 0x0003FFFF;
} else if(keyconf == PSP_INIT_KEYCONFIG_POPS) {
var = 0x0003FFFF;
} else {
var = 0x0003F3F9;
}
option.g0x00002B60 = 0;
option.g0x00002B5C = option.g0x00002B58;
option.g0x00002B58 = var;
int var26;
switch(sceKernelGetModel()){
case 0:
case 1:
case 2:
case 3:
case 6:
case 8:
case 10:
var26 = 0x01FFF3F9;
break;
case 4:
case 5:
case 7:
case 9:
var26 = 0x39FFF3F9;
break;
}
option.g0x00002B64 = var26;
option.g0x00002B68 = 0x39FFF3F9;
option.g0x00002B6C = 0x00F1F3F9;
option.g0x00002B70 = 0x390E0000;
option.g0x00002B74 = 0;
option.g0x00002B78 = 0x81;
option.g0x00002B7C = 0x81;
}
return 0;
}
int sceCtrlEnd() {
sceKernelUnregisterSysEventHandler(sysevent);
sceSyscon_driver_62012EAF(1);
sceDisplayWaitVblankStart();
option.timer = -1;
if(option.timer >= 0) {
sceSTimerStopCount(option.timer);
sceSTimerFree(option.timer);
}
sceKernelReleaseSubIntrHandlerFunction(/*PSP_DISPLAY_SUBINT, PSP_THREAD0_INT*/0x1E, 0x13);
sceKernelDeleteEventFlag(option.evid);
while(option.g0x000028D0)
sceDisplayWaitVblankStart();
return 0;
}
int sceCtrlSuspend() {
if(option.cycle != 0) {
sceSTimerStopCount(option.timer);
} else {
sceKernelDisableSubIntr(PSP_DISPLAY_SUBINT,/*PSP_THREAD0_INT*/ 0x13);
}
return 0;
}
void sub_00000968(int arg1, char arg2, char arg3) {
unsigned int timelow = sceKernelGetSystemTimeLow();
}
SceUInt sub_00001E0C (void *common) { //(alarm handler)
int intr = sceKernelCpuSuspendIntr();
int var1;
if(option.g0x000028D8 != 0) var1 = option.g0x00002AEC;
else asm("ins $a0, $zr, 0, 17" : "=r" (var1));
sub_00000968(ptr, option.g0x00002B04, option.g0x00002B05);
sceKernelSetEventFlag(option.evid, 1);
sceKernelCpuResumeIntr(intr);
return 0;
}
int sub_00000528 () { //not complete (handler)
int intr = sceKernelCpuSuspendIntr();
if(option.cycle != 0) {
if(option.g0x000028D0 == 0 && option.polling != 0) {
option.g0x000028D0 = 1;
option.packet.tx_cmd = 7;
if(option.sampling[0] != 0 && option.sampling[1] != 0)
option.packet.tx_cmd = 8;
option.packet.tx_len = 2;
int ret = sceSysconCmdExecAsync(option.packet, 1, 0x610, 0);
if(ret < 0) option.g0x000028D0 = 0;
} else {
sceKernelSetAlarm(0x2BC, sub_00001E0C, NULL);
}
}
if(option.g0x000028D4 != 0) {
option.g0x000028D4 = 0;
sceKernelPowerTick(0);
}
sceKernelCpuResumeIntr(intr);
return -1;
}
int sceCtrlResume() {//not complete
int var3 = sceSyscon_driver_4717A520();
if(var3 == 0)
//asm("ins %0, $zr, 29, 1" : "=r" (option.g0x00002AF8));
INS(option.g0x00002AF8, 0, 29, 1);
else if(var3 == 1)
option.g0x00002AF8 |= 0x20000000;
option.g0x000028CF = -1;
if(option.cycle != 0) {
sceSTimerStartCount(option.timer);
sceSTimerSetHandler(option.timer, option.cycle, sub_00000528, 0);
} else {
sceKernelReleaseSubIntrHandlerFunction(/*PSP_DISPLAY_SUBINT, PSP_THREAD0_INT*/0x1E, 0x13);
}
return 0;
}
int sceCtrlSetSamplingMode(int mode) {
int ret = 0x80000107;
if(mode < 2) {
int intr = sceKernelCpuSuspendIntr();
int k1;
GET_K1(k1);
int i = k1 >> 20 & 1;
ret = sampling[i];
sampling[i] = mode;
sceKernelCpuResumeIntr(intr);
}
return ret;
}
int sceCtrlGetSamplingMode(int *pmode) {
int k1;
GET_K1(k1);
SLL_K1(11);
int i = (k1 >> 31 < 1) ? 1 : 0;
if(k1 & pmode >= 0) {
pmode = sampling[i];
}
SET_K1(k1);
return 0;
}
int sceCtrl_driver_A81235E5(int arg) {
option.g0x00002B07 = arg;
return 0;
}
int sceCtrl_driver_7A6436DE(int arg) {
option.g0x00002BC0 = arg;
return 0;
}
int sceCtrlSetPollingMode(char arg) {
option.polling = arg;
return 0;
}
int sceCtrl_driver_5BE1D4F2() {
option.g0x000028CE = 1;
return 0;
}
int sceCtrlSetIdleCancelKey(int arg1, int arg2, int arg3, int arg4) {
g0x00002B68 = arg1;
g0x00002B6C = arg2;
g0x00002B70 = arg3;
g0x00002B74 = arg4;
return 0;
}
int sceCtrlSetRapidFire(arg1, arg2, arg3, arg4, arg5, arg6, arg7) {//not complete
return 0;
}
int sceCtrlClearRapidFire(char key) {
if(key >= 16) return 0x80000102;
option.rapidfire[key] = 0;
return 0;
}
int sceCtrl_348D99D4(short arg1) {//set
if(arg1 >= 301) return 0x800001FE;
option.g0x000028D6 = (arg1 ^ 0x1) ? arg1 : 0;
return 0;
}
int sceCtrl_AF5960F3() {//get
return option.g0x000028D6;
}
/**
* no 0,1,2,3
*
*/
int sceCtrl_driver_D8329216(int no, int arg2, int arg3, int arg4) {
if(no >= 4) return 0x800001FE;
//v0 = no + no << 2;
//t1 = g0x00002B00 + (v0 << 2);
//g0x00002B0C;
return 0;
}
int sceCtrlRegisterButtonCallback(int no, unsigned int mask, void (*cb)(int, int, void*), void *arg) {
if(no > 3) return 0x80000102;
int intr = sceKernelCpuSuspendIntr();
option.callback[no].mask = mask;
option.callback[no].cb = cb;
MOVE(option.callback[no].gp, "$gp");
option.callback[no].arg = arg;
sceKernelCpuResumeIntr(intr);
return 0;
}
int sceCtrlGetSamplingCycle(int *pcycle) {
int k1;
GET_K1(k1);
SLL_K1(11);
if(k1 & pcycle >= 0)
pcycle = option.cycle;
SET_K1(k1);
return 0;
}
int sub_00001EA4(SceCtrlData *pad_data, int count, int arg3, int mode) { //not complete
if(count > 64) return 0x80000104;
if(arg3 >= 3) return 0x800001FE;
if(arg3 == 2) return 0x80000004;//GENERIC ERROR
int k1;
GET_K1(k1);
SLL_K1(11);
if(k1 & pad_data < 0) return 0x80000023;
if(k1 < 0) {
//if(
}
SET_K1(k1);
return 0;
}
int sceCtrlPeekBufferPositive(SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, 0, 0);
}
int sceCtrlPeekBufferNegative(SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, 0, 1);
}
int sceCtrlReadBufferPositive(SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, 0, 2);
}
int sceCtrlReadBufferNegative(SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, 0, 3);
}
int sceCtrl_5A36B1C2(int arg1, SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, arg1, 4);
}
int sceCtrl_239A6BA7(int arg1, SceCtrlData *pad_data, int count) {
return sub_00001EA4 (pad_data, count, arg1, 5);
}
int sceCtrl_1098030B(int arg1, SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, arg1, 6);
}
int sceCtrl_7C3675AB(int arg1, SceCtrlData *pad_data, int count) {
return sub_00001EA4(pad_data, count, arg1, 7);
}