aboutsummaryrefslogtreecommitdiff
path: root/frontend/plugin.c
blob: 6bb9aa497b8d8c4c8495ebd6f823b3b9b6f6122d (plain)
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
/*
 * (C) notaz, 2010
 *
 * This work is licensed under the terms of the GNU GPLv2 or later.
 * See the COPYING file in the top-level directory.
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "plugin_lib.h"
#include "plugin.h"
#include "psemu_plugin_defs.h"
#include "../libpcsxcore/system.h"
#include "../plugins/cdrcimg/cdrcimg.h"

#ifndef _WIN32
#define CALLBACK
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

static int dummy_func() {
	return 0;
}

/* SPU */
extern long CALLBACK SPUopen(void);
extern long CALLBACK SPUinit(void);
extern long CALLBACK SPUshutdown(void);
extern long CALLBACK SPUclose(void);
extern void CALLBACK SPUplaySample(unsigned char);
extern void CALLBACK SPUwriteRegister(unsigned long, unsigned short, unsigned int);
extern unsigned short CALLBACK SPUreadRegister(unsigned long);
extern void CALLBACK SPUwriteDMA(unsigned short);
extern unsigned short CALLBACK SPUreadDMA(void);
extern void CALLBACK SPUwriteDMAMem(unsigned short *, int, unsigned int);
extern void CALLBACK SPUreadDMAMem(unsigned short *, int, unsigned int);
extern void CALLBACK SPUplayADPCMchannel(void *);
extern void CALLBACK SPUregisterCallback(void (*cb)(void));
extern void CALLBACK SPUregisterScheduleCb(void (*cb)(unsigned int));
extern long CALLBACK SPUconfigure(void);
extern long CALLBACK SPUtest(void);
extern void CALLBACK SPUabout(void);
extern long CALLBACK SPUfreeze(unsigned int, void *, unsigned int);
extern void CALLBACK SPUasync(unsigned int, unsigned int);
extern int  CALLBACK SPUplayCDDAchannel(short *, int);

/* PAD */
static long PADreadPort1(PadDataS *pad) {
	int pad_index = pad->requestPadIndex;
    pad->controllerType = in_type[pad_index];
    pad->buttonStatus = ~in_keystate[pad_index];
    if (multitap1 == 1)
    	pad->portMultitap = 1;
    else
    	pad->portMultitap = 0;
    
    if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON)
    {
        pad->leftJoyX = in_analog_left[pad_index][0];
        pad->leftJoyY = in_analog_left[pad_index][1];
        pad->rightJoyX = in_analog_right[pad_index][0];
        pad->rightJoyY = in_analog_right[pad_index][1];
    }
    return 0;
}

static long PADreadPort2(PadDataS *pad) {
	int pad_index = pad->requestPadIndex;
    
    pad->controllerType = in_type[pad_index];
    pad->buttonStatus = ~in_keystate[pad_index];
    if (multitap2 == 1)
    	pad->portMultitap = 2;
    else
    	pad->portMultitap = 0;
    
    if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON)
    {
        pad->leftJoyX = in_analog_left[pad_index][0];
        pad->leftJoyY = in_analog_left[pad_index][1];
        pad->rightJoyX = in_analog_right[pad_index][0];
        pad->rightJoyY = in_analog_right[pad_index][1];
    }
    return 0;
}

/* GPU */
extern long GPUopen(unsigned long *, char *, char *);
extern long GPUinit(void);
extern long GPUshutdown(void);
extern long GPUclose(void);
extern void GPUwriteStatus(uint32_t);
extern void GPUwriteData(uint32_t);
extern void GPUwriteDataMem(uint32_t *, int);
extern uint32_t GPUreadStatus(void);
extern uint32_t GPUreadData(void);
extern void GPUreadDataMem(uint32_t *, int);
extern long GPUdmaChain(uint32_t *,uint32_t);
extern void GPUupdateLace(void);
extern long GPUfreeze(uint32_t, void *);
extern void GPUvBlank(int, int);
extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);


#define DUMMY(id, name) \
	{ id, #name, dummy_func }

#define DIRECT(id, name) \
	{ id, #name, name }

#define DUMMY_GPU(name)  DUMMY(PLUGIN_GPU, name)
#define DUMMY_CDR(name)  DUMMY(PLUGIN_CDR, name)
#define DUMMY_PAD(name)  DUMMY(PLUGIN_PAD, name)
#define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
#define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
#define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)

static const struct {
	int id;
	const char *name;
	void *func;
} plugin_funcs[] = {
	/* CDR */
	DUMMY_CDR(CDRinit),
	DUMMY_CDR(CDRshutdown),
	DUMMY_CDR(CDRopen),
	DUMMY_CDR(CDRclose),
	DUMMY_CDR(CDRtest),
	DUMMY_CDR(CDRgetTN),
	DUMMY_CDR(CDRgetTD),
	DUMMY_CDR(CDRreadTrack),
	DUMMY_CDR(CDRgetBuffer),
	DUMMY_CDR(CDRgetBufferSub),
	DUMMY_CDR(CDRplay),
	DUMMY_CDR(CDRstop),
	DUMMY_CDR(CDRgetStatus),
	DUMMY_CDR(CDRgetDriveLetter),
	DUMMY_CDR(CDRconfigure),
	DUMMY_CDR(CDRabout),
	DUMMY_CDR(CDRsetfilename),
	DUMMY_CDR(CDRreadCDDA),
	DUMMY_CDR(CDRgetTE),
	/* SPU */
	DIRECT_SPU(SPUconfigure),
	DIRECT_SPU(SPUabout),
	DIRECT_SPU(SPUinit),
	DIRECT_SPU(SPUshutdown),
	DIRECT_SPU(SPUtest),
	DIRECT_SPU(SPUopen),
	DIRECT_SPU(SPUclose),
//	DIRECT_SPU(SPUplaySample), // unused?
	DIRECT_SPU(SPUwriteRegister),
	DIRECT_SPU(SPUreadRegister),
	DIRECT_SPU(SPUwriteDMA),
	DIRECT_SPU(SPUreadDMA),
	DIRECT_SPU(SPUwriteDMAMem),
	DIRECT_SPU(SPUreadDMAMem),
	DIRECT_SPU(SPUplayADPCMchannel),
	DIRECT_SPU(SPUfreeze),
	DIRECT_SPU(SPUregisterCallback),
	DIRECT_SPU(SPUregisterScheduleCb),
	DIRECT_SPU(SPUasync),
	DIRECT_SPU(SPUplayCDDAchannel),
	/* PAD */
	DUMMY_PAD(PADinit),
	DUMMY_PAD(PADshutdown),
	DUMMY_PAD(PADopen),
	DUMMY_PAD(PADclose),
	DUMMY_PAD(PADsetSensitive),
	DIRECT_PAD(PADreadPort1),
	DIRECT_PAD(PADreadPort2),
/*
	DUMMY_PAD(PADquery),
	DUMMY_PAD(PADconfigure),
	DUMMY_PAD(PADtest),
	DUMMY_PAD(PADabout),
	DUMMY_PAD(PADkeypressed),
	DUMMY_PAD(PADstartPoll),
	DUMMY_PAD(PADpoll),
*/
	/* GPU */
	DIRECT_GPU(GPUupdateLace),
	DIRECT_GPU(GPUinit),
	DIRECT_GPU(GPUshutdown),
	DIRECT_GPU(GPUopen),
	DIRECT_GPU(GPUclose),
	DIRECT_GPU(GPUreadStatus),
	DIRECT_GPU(GPUreadData),
	DIRECT_GPU(GPUreadDataMem),
	DIRECT_GPU(GPUwriteStatus),
	DIRECT_GPU(GPUwriteData),
	DIRECT_GPU(GPUwriteDataMem),
	DIRECT_GPU(GPUdmaChain),
	DIRECT_GPU(GPUfreeze),
	DIRECT_GPU(GPUvBlank),
	DIRECT_GPU(GPUrearmedCallbacks),

	DUMMY_GPU(GPUdisplayText),
/*
	DIRECT_GPU(GPUkeypressed),
	DIRECT_GPU(GPUmakeSnapshot),
	DIRECT_GPU(GPUconfigure),
	DIRECT_GPU(GPUtest),
	DIRECT_GPU(GPUabout),
	DIRECT_GPU(GPUgetScreenPic),
	DIRECT_GPU(GPUshowScreenPic),
*/
//	DIRECT_GPU(GPUclearDynarec),
};

void *plugin_link(enum builtint_plugins_e id, const char *sym)
{
	int i;

	if (id == PLUGIN_CDRCIMG)
		return cdrcimg_get_sym(sym);

	for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
		if (id != plugin_funcs[i].id)
			continue;

		if (strcmp(sym, plugin_funcs[i].name) != 0)
			continue;

		return plugin_funcs[i].func;
	}

	//fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
	return NULL;
}

void plugin_call_rearmed_cbs(void)
{
	extern void *hGPUDriver;
	void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);

	rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
	if (rearmed_set_cbs != NULL)
		rearmed_set_cbs(&pl_rearmed_cbs);
}

#ifdef PCNT

/* basic profile stuff */
#include "pcnt.h"

unsigned int pcounters[PCNT_CNT];
unsigned int pcounter_starts[PCNT_CNT];

#define pc_hook_func(name, args, pargs, cnt) \
extern void (*name) args; \
static void (*o_##name) args; \
static void w_##name args \
{ \
	unsigned int pc_start = pcnt_get(); \
	o_##name pargs; \
	pcounters[cnt] += pcnt_get() - pc_start; \
}

#define pc_hook_func_ret(retn, name, args, pargs, cnt) \
extern retn (*name) args; \
static retn (*o_##name) args; \
static retn w_##name args \
{ \
	retn ret; \
	unsigned int pc_start = pcnt_get(); \
	ret = o_##name pargs; \
	pcounters[cnt] += pcnt_get() - pc_start; \
	return ret; \
}

pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)

pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
pc_hook_func              (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
pc_hook_func              (SPU_playADPCMchannel, (void *a0), (a0), PCNT_SPU)
pc_hook_func              (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1), (a0, a1), PCNT_SPU)

#define hook_it(name) { \
	o_##name = name; \
	name = w_##name; \
}

void pcnt_hook_plugins(void)
{
	pcnt_init();

	hook_it(GPU_writeStatus);
	hook_it(GPU_writeData);
	hook_it(GPU_writeDataMem);
	hook_it(GPU_readStatus);
	hook_it(GPU_readData);
	hook_it(GPU_readDataMem);
	hook_it(GPU_dmaChain);
	hook_it(GPU_updateLace);
	hook_it(SPU_writeRegister);
	hook_it(SPU_readRegister);
	hook_it(SPU_writeDMA);
	hook_it(SPU_readDMA);
	hook_it(SPU_writeDMAMem);
	hook_it(SPU_readDMAMem);
	hook_it(SPU_playADPCMchannel);
	hook_it(SPU_async);
	hook_it(SPU_playCDDAchannel);
}

// hooked into recompiler
void pcnt_gte_start(int op)
{
	pcnt_start(PCNT_GTE);
}

void pcnt_gte_end(int op)
{
	pcnt_end(PCNT_GTE);
}

#endif