Source

Open PS2 Loader / src / pad.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
/*
  Copyright 2009, Ifcaro, Volca
  Licenced under Academic Free License version 3.0
  Review OpenUsbLd README & LICENSE files for further details.  
*/

#include "include/usbld.h"
#include "include/pad.h"
#include "include/ioman.h"
#include <libpad.h>
#include <timer.h>
#include <time.h>

#define MAX_PADS 4

// Cpu ticks per one milisecond
#define CLOCKS_PER_MILISEC 147456

// 200 ms per repeat
#define DEFAULT_PAD_DELAY 200

struct pad_data_t {
	int port, slot;
	u32 paddata;
	u32 oldpaddata;
	struct padButtonStatus buttons;

	// pad_dma_buf is provided by the user, one buf for each pad
	// contains the pad's current state
	char padBuf[256] __attribute__((aligned(64)));

	char actAlign[6];
	int actuators;
};

/// current time in miliseconds (last update time)
static u32 curtime = 0;
static u32 time_since_last = 0;

static unsigned short pad_count;
static struct pad_data_t pad_data[MAX_PADS];

// gathered pad data
static u32 paddata;
static u32 oldpaddata;

static int delaycnt[16];
static int paddelay[16];

// KEY_ to PAD_ conversion table
static const int keyToPad[17] = {
	-1,
	PAD_LEFT,
	PAD_DOWN,
	PAD_RIGHT,
	PAD_UP,
	PAD_START,
	PAD_R3,
	PAD_L3,
	PAD_SELECT,
	PAD_SQUARE,
	PAD_CROSS,
	PAD_CIRCLE,
	PAD_TRIANGLE,
	PAD_R1,
	PAD_L1,
	PAD_R2,
	PAD_L2
};

/*
 * waitPadReady()
 */
static int waitPadReady(struct pad_data_t* pad) {
	int state;
	
	// busy wait for the pad to get ready
	do {
		state = padGetState(pad->port, pad->slot);
	} while((state != PAD_STATE_STABLE) && (state != PAD_STATE_FINDCTP1) && (state != PAD_STATE_DISCONN));
    	
	return state;
};

static int initializePad(struct pad_data_t* pad) {
	int tmp;
	int modes;
	int i;
	
	// is there any device connected to that port?
	if (waitPadReady(pad) == PAD_STATE_DISCONN) {
		return 1; // nope, don't waste your time here!
	}
	
	// How many different modes can this device operate in?
	// i.e. get # entrys in the modetable
	modes = padInfoMode(pad->port, pad->slot, PAD_MODETABLE, -1);
	LOG("PAD The device has %d modes: ", modes);

	if (modes > 0) {
		LOG("( ");
		
		for (i = 0; i < modes; i++) {
			tmp = padInfoMode(pad->port, pad->slot, PAD_MODETABLE, i);
			LOG("%d ", tmp);
		}
	        
	        LOG(")\n");
	}

	tmp = padInfoMode(pad->port, pad->slot, PAD_MODECURID, 0);
	LOG("PAD It is currently using mode %d\n", tmp);

	// If modes == 0, this is not a Dual shock controller 
	// (it has no actuator engines)
	if (modes == 0) {
		LOG("PAD This is a digital controller?\n");
		return 1;
	}

	// Verify that the controller has a DUAL SHOCK mode
	i = 0;
	do {
		if (padInfoMode(pad->port, pad->slot, PAD_MODETABLE, i) == PAD_TYPE_DUALSHOCK)
			break;
		i++;
	} while (i < modes);
	
	if (i >= modes) {
		LOG("PAD This is no Dual Shock controller\n");
		return 1;
	}

	// If ExId != 0x0 => This controller has actuator engines
	// This check should always pass if the Dual Shock test above passed
	tmp = padInfoMode(pad->port, pad->slot, PAD_MODECUREXID, 0);
	if (tmp == 0) {
	        LOG("PAD This is no Dual Shock controller??\n");
	        return 1;
	}

	LOG("PAD Enabling dual shock functions\n");

	// When using MMODE_LOCK, user cant change mode with Select button
	padSetMainMode(pad->port, pad->slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK);

	waitPadReady(pad);
	tmp = padInfoPressMode(pad->port, pad->slot);
	LOG("PAD infoPressMode: %d\n", tmp);

	waitPadReady(pad);        
	tmp = padEnterPressMode(pad->port, pad->slot);
	LOG("PAD enterPressMode: %d\n", tmp);

	waitPadReady(pad);
	pad->actuators = padInfoAct(pad->port, pad->slot, -1, 0);
	LOG("PAD # of actuators: %d\n", pad->actuators);

	if (pad->actuators != 0) {
		pad->actAlign[0] = 0;   // Enable small engine
		pad->actAlign[1] = 1;   // Enable big engine
		pad->actAlign[2] = 0xff;
		pad->actAlign[3] = 0xff;
		pad->actAlign[4] = 0xff;
		pad->actAlign[5] = 0xff;

		waitPadReady(pad);
		tmp = padSetActAlign(pad->port, pad->slot, pad->actAlign);
		LOG("PAD padSetActAlign: %d\n", tmp);
	} else {
		LOG("PAD Did not find any actuators.\n");
	}

	waitPadReady(pad);

	return 1;
}

static int readPad(struct pad_data_t* pad)
{
	int rcode = 0;

	int ret = padRead(pad->port, pad->slot, &pad->buttons); // port, slot, buttons
            
        if (ret != 0)
	{
		u32 newpdata = 0xffff ^ pad->buttons.btns; 
		
		if (newpdata != 0x0) // something
			rcode = 1;
		else
			rcode = 0;

		pad->oldpaddata = pad->paddata;
		pad->paddata = newpdata;
		
		// merge into the global vars
		paddata |= pad->paddata;
	}

	return rcode;
}

/** Returns delay (in miliseconds) specified for the given key.
* @param id The button id
* @param repeat Boolean value specifying if we want initial key delay (0) or the repeat key delay (1)
* @return the delay to the next key event
*/
static int getKeyDelay(int id, int repeat) {
	int delay = paddelay[id - 1];
	
	// if not in repeat, the delay is enlarged
	if (!repeat)
		delay *= 3;
	
	return delay;
}

/** polling method. Call every frame. */
int readPads() {
	int i;
	oldpaddata = paddata;
	paddata = 0;
	
	// in ms.
	u32 newtime = cpu_ticks() / CLOCKS_PER_MILISEC;
	time_since_last = newtime - curtime;
	curtime = newtime;
	
	int rslt = 0;
	
	for (i = 0; i < pad_count; ++i) {
		rslt |= readPad(&pad_data[i]);
	}
	
	for (i = 0; i < 16; ++i) {
		if (getKeyPressed(i + 1))
			delaycnt[i] -= time_since_last;
		else
			delaycnt[i] = getKeyDelay(i + 1, 0);
	}
	
	return rslt;
}

/** Key getter with key repeats.
* @param id The button ID
* @return nonzero if button is being pressed just now
*/
int getKey(int id) {
	if ( (id <= 0) || (id >= 17) )
		return 0;

	int kid = id - 1;
	
	// either the button was not pressed this frame, then reset counter and return
	// or it was, then handle the repetition
	if (getKeyOn(id)) {
		delaycnt[kid] = getKeyDelay(id, 0);
		return 1;
	}

	if(!getKeyPressed(id))
		return 0;

	if(delaycnt[kid] <= 0) {
		delaycnt[kid] = getKeyDelay(id, 1);
		return 1;
	}
	
	return 0;
}

/** Detects key-on event. Returns true if the button was not pressed the last frame but is pressed this frame.
* @param id The button ID
* @return nonzero if button is being pressed just now
*/
int getKeyOn(int id) {
	if ( (id<=0) || (id>=17) )
		return 0;
	
	// old v.s. new pad data
	int keyid = keyToPad[id];
	
	return (paddata & keyid) && (!(oldpaddata & keyid));
}

/** Detects key-off event. Returns true if the button was pressed the last frame but is not pressed this frame.
* @param id The button ID
* @return nonzero if button is being released
*/
int getKeyOff(int id) {
	if ( (id<=0) || (id>=17) )
		return 0;
	
	// old v.s. new pad data
	int keyid = keyToPad[id];
	
	return (!(paddata & keyid)) && (oldpaddata & keyid);
}

/** Returns true (nonzero) if the button is currently pressed
* @param id The button ID
* @return nonzero if button is being held
*/
int getKeyPressed(int id) {
	// old v.s. new pad data
	int keyid = keyToPad[id];
	
	return (paddata & keyid);
}

/** Sets the delay to wait for button repetition event to occur.
* @param button The button ID
* @param btndelay The button delay (in query count)
*/
void setButtonDelay(int button, int btndelay) {
	if ( (button<=0) || (button>=17) )
		return;
	
	paddelay[button-1] = btndelay;
}

int getButtonDelay(int button) {
	if ( (button<=0) || (button>=17) )
		return 0;
	
	return paddelay[button-1];
}

/** Unloads a single pad. 
* @see unloadPads */
static void unloadPad(struct pad_data_t* pad) {
	padPortClose(pad->port, pad->slot);
}

/** Unloads all pads. Use to terminate the usage of the pads. */
void unloadPads() {
	int i;
	
	for (i = 0; i < pad_count; ++i)
		unloadPad(&pad_data[i]);
	
	padReset();
}

/** Tries to start a single pad.
* @param pad The pad data holding structure 
* @return 0 Error, != 0 Ok */
static int startPad(struct pad_data_t* pad) {
	if(padPortOpen(pad->port, pad->slot, pad->padBuf) == 0) {
		return 0;
	}
	
	if(!initializePad(pad)) {
	        return 0;
	}

	waitPadReady(pad);
	return 1;
}

/** Starts all pads. 
* @return Count of dual shock compatible pads. 0 if none present. */
int startPads() {
	// scan for pads that exist... at least one has to be present
	pad_count = 0;

	int maxports = padGetPortMax();

	int port; // 0 -> Connector 1, 1 -> Connector 2
	int slot; // Always zero if not using multitap
		
	for (port = 0; port < maxports; ++port) {
		int maxslots = padGetSlotMax(port);
		
		for (slot = 0; slot < maxslots; ++slot) {
		
			struct pad_data_t* cpad = &pad_data[pad_count];
			
			cpad->port = port;
			cpad->slot = slot;
			
			if (startPad(cpad))
				++pad_count;
		}
		
		if (pad_count >= MAX_PADS)
			break; // enough already!
	}
	
	int n;
	for(n=0; n<16; ++n) {
		delaycnt[n]=DEFAULT_PAD_DELAY;
		paddelay[n]=DEFAULT_PAD_DELAY;
	}
	
	return pad_count;
}

void padStoreSettings(int* buffer) {
	int i;
	
	for (i = 0; i < 16; i++)
		buffer[i] = paddelay[i];
}


void padRestoreSettings(int* buffer) {
	int i;
	
	for (i = 0; i < 16; i++)
		paddelay[i] = buffer[i];
}