Merge pull request #321 from fpscan/master
[pcsx_rearmed.git] / frontend / plugin.c
1 /*
2  * (C) notaz, 2010
3  *
4  * This work is licensed under the terms of the GNU GPLv2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11
12 #include "plugin_lib.h"
13 #include "plugin.h"
14 #include "psemu_plugin_defs.h"
15 #include "../libpcsxcore/system.h"
16 #include "../plugins/cdrcimg/cdrcimg.h"
17
18 #ifndef _WIN32
19 #define CALLBACK
20 #else
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #endif
24
25 static int dummy_func() {
26         return 0;
27 }
28
29 /* SPU */
30 extern long CALLBACK SPUopen(void);
31 extern long CALLBACK SPUinit(void);
32 extern long CALLBACK SPUshutdown(void);
33 extern long CALLBACK SPUclose(void);
34 extern void CALLBACK SPUplaySample(unsigned char);
35 extern void CALLBACK SPUwriteRegister(unsigned long, unsigned short, unsigned int);
36 extern unsigned short CALLBACK SPUreadRegister(unsigned long);
37 extern void CALLBACK SPUwriteDMA(unsigned short);
38 extern unsigned short CALLBACK SPUreadDMA(void);
39 extern void CALLBACK SPUwriteDMAMem(unsigned short *, int, unsigned int);
40 extern void CALLBACK SPUreadDMAMem(unsigned short *, int, unsigned int);
41 extern void CALLBACK SPUplayADPCMchannel(void *);
42 extern void CALLBACK SPUregisterCallback(void (*cb)(void));
43 extern void CALLBACK SPUregisterScheduleCb(void (*cb)(unsigned int));
44 extern long CALLBACK SPUconfigure(void);
45 extern long CALLBACK SPUtest(void);
46 extern void CALLBACK SPUabout(void);
47 extern long CALLBACK SPUfreeze(unsigned int, void *, unsigned int);
48 extern void CALLBACK SPUasync(unsigned int, unsigned int);
49 extern int  CALLBACK SPUplayCDDAchannel(short *, int);
50
51 /* PAD */
52 static long PADreadPort1(PadDataS *pad) {
53         int pad_index = pad->requestPadIndex;
54     pad->controllerType = in_type[pad_index];
55     pad->buttonStatus = ~in_keystate[pad_index];
56     if (multitap1 == 1)
57         pad->portMultitap = 1;
58     else
59         pad->portMultitap = 0;
60     
61     if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGJOY || in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON)
62     {
63         pad->leftJoyX = in_analog_left[pad_index][0];
64         pad->leftJoyY = in_analog_left[pad_index][1];
65         pad->rightJoyX = in_analog_right[pad_index][0];
66         pad->rightJoyY = in_analog_right[pad_index][1];
67     }
68     return 0;
69 }
70
71 static long PADreadPort2(PadDataS *pad) {
72         int pad_index = pad->requestPadIndex;
73     
74     pad->controllerType = in_type[pad_index];
75     pad->buttonStatus = ~in_keystate[pad_index];
76     if (multitap2 == 1)
77         pad->portMultitap = 2;
78     else
79         pad->portMultitap = 0;
80     
81     if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGJOY || in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON)
82     {
83         pad->leftJoyX = in_analog_left[pad_index][0];
84         pad->leftJoyY = in_analog_left[pad_index][1];
85         pad->rightJoyX = in_analog_right[pad_index][0];
86         pad->rightJoyY = in_analog_right[pad_index][1];
87     }
88     return 0;
89 }
90
91 /* GPU */
92 extern long GPUopen(unsigned long *, char *, char *);
93 extern long GPUinit(void);
94 extern long GPUshutdown(void);
95 extern long GPUclose(void);
96 extern void GPUwriteStatus(uint32_t);
97 extern void GPUwriteData(uint32_t);
98 extern void GPUwriteDataMem(uint32_t *, int);
99 extern uint32_t GPUreadStatus(void);
100 extern uint32_t GPUreadData(void);
101 extern void GPUreadDataMem(uint32_t *, int);
102 extern long GPUdmaChain(uint32_t *,uint32_t);
103 extern void GPUupdateLace(void);
104 extern long GPUfreeze(uint32_t, void *);
105 extern void GPUvBlank(int, int);
106 extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
107
108
109 #define DUMMY(id, name) \
110         { id, #name, dummy_func }
111
112 #define DIRECT(id, name) \
113         { id, #name, name }
114
115 #define DUMMY_GPU(name)  DUMMY(PLUGIN_GPU, name)
116 #define DUMMY_CDR(name)  DUMMY(PLUGIN_CDR, name)
117 #define DUMMY_PAD(name)  DUMMY(PLUGIN_PAD, name)
118 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
119 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
120 #define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
121
122 static const struct {
123         int id;
124         const char *name;
125         void *func;
126 } plugin_funcs[] = {
127         /* CDR */
128         DUMMY_CDR(CDRinit),
129         DUMMY_CDR(CDRshutdown),
130         DUMMY_CDR(CDRopen),
131         DUMMY_CDR(CDRclose),
132         DUMMY_CDR(CDRtest),
133         DUMMY_CDR(CDRgetTN),
134         DUMMY_CDR(CDRgetTD),
135         DUMMY_CDR(CDRreadTrack),
136         DUMMY_CDR(CDRgetBuffer),
137         DUMMY_CDR(CDRgetBufferSub),
138         DUMMY_CDR(CDRplay),
139         DUMMY_CDR(CDRstop),
140         DUMMY_CDR(CDRgetStatus),
141         DUMMY_CDR(CDRgetDriveLetter),
142         DUMMY_CDR(CDRconfigure),
143         DUMMY_CDR(CDRabout),
144         DUMMY_CDR(CDRsetfilename),
145         DUMMY_CDR(CDRreadCDDA),
146         DUMMY_CDR(CDRgetTE),
147         /* SPU */
148         DIRECT_SPU(SPUconfigure),
149         DIRECT_SPU(SPUabout),
150         DIRECT_SPU(SPUinit),
151         DIRECT_SPU(SPUshutdown),
152         DIRECT_SPU(SPUtest),
153         DIRECT_SPU(SPUopen),
154         DIRECT_SPU(SPUclose),
155 //      DIRECT_SPU(SPUplaySample), // unused?
156         DIRECT_SPU(SPUwriteRegister),
157         DIRECT_SPU(SPUreadRegister),
158         DIRECT_SPU(SPUwriteDMA),
159         DIRECT_SPU(SPUreadDMA),
160         DIRECT_SPU(SPUwriteDMAMem),
161         DIRECT_SPU(SPUreadDMAMem),
162         DIRECT_SPU(SPUplayADPCMchannel),
163         DIRECT_SPU(SPUfreeze),
164         DIRECT_SPU(SPUregisterCallback),
165         DIRECT_SPU(SPUregisterScheduleCb),
166         DIRECT_SPU(SPUasync),
167         DIRECT_SPU(SPUplayCDDAchannel),
168         /* PAD */
169         DUMMY_PAD(PADinit),
170         DUMMY_PAD(PADshutdown),
171         DUMMY_PAD(PADopen),
172         DUMMY_PAD(PADclose),
173         DUMMY_PAD(PADsetSensitive),
174         DIRECT_PAD(PADreadPort1),
175         DIRECT_PAD(PADreadPort2),
176 /*
177         DUMMY_PAD(PADquery),
178         DUMMY_PAD(PADconfigure),
179         DUMMY_PAD(PADtest),
180         DUMMY_PAD(PADabout),
181         DUMMY_PAD(PADkeypressed),
182         DUMMY_PAD(PADstartPoll),
183         DUMMY_PAD(PADpoll),
184 */
185         /* GPU */
186         DIRECT_GPU(GPUupdateLace),
187         DIRECT_GPU(GPUinit),
188         DIRECT_GPU(GPUshutdown),
189         DIRECT_GPU(GPUopen),
190         DIRECT_GPU(GPUclose),
191         DIRECT_GPU(GPUreadStatus),
192         DIRECT_GPU(GPUreadData),
193         DIRECT_GPU(GPUreadDataMem),
194         DIRECT_GPU(GPUwriteStatus),
195         DIRECT_GPU(GPUwriteData),
196         DIRECT_GPU(GPUwriteDataMem),
197         DIRECT_GPU(GPUdmaChain),
198         DIRECT_GPU(GPUfreeze),
199         DIRECT_GPU(GPUvBlank),
200         DIRECT_GPU(GPUrearmedCallbacks),
201
202         DUMMY_GPU(GPUdisplayText),
203 /*
204         DIRECT_GPU(GPUkeypressed),
205         DIRECT_GPU(GPUmakeSnapshot),
206         DIRECT_GPU(GPUconfigure),
207         DIRECT_GPU(GPUtest),
208         DIRECT_GPU(GPUabout),
209         DIRECT_GPU(GPUgetScreenPic),
210         DIRECT_GPU(GPUshowScreenPic),
211 */
212 //      DIRECT_GPU(GPUclearDynarec),
213 };
214
215 void *plugin_link(enum builtint_plugins_e id, const char *sym)
216 {
217         int i;
218
219         if (id == PLUGIN_CDRCIMG)
220                 return cdrcimg_get_sym(sym);
221
222         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
223                 if (id != plugin_funcs[i].id)
224                         continue;
225
226                 if (strcmp(sym, plugin_funcs[i].name) != 0)
227                         continue;
228
229                 return plugin_funcs[i].func;
230         }
231
232         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
233         return NULL;
234 }
235
236 void plugin_call_rearmed_cbs(void)
237 {
238         extern void *hGPUDriver;
239         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
240
241         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
242         if (rearmed_set_cbs != NULL)
243                 rearmed_set_cbs(&pl_rearmed_cbs);
244 }
245
246 #ifdef PCNT
247
248 /* basic profile stuff */
249 #include "pcnt.h"
250
251 unsigned int pcounters[PCNT_CNT];
252 unsigned int pcounter_starts[PCNT_CNT];
253
254 #define pc_hook_func(name, args, pargs, cnt) \
255 extern void (*name) args; \
256 static void (*o_##name) args; \
257 static void w_##name args \
258 { \
259         unsigned int pc_start = pcnt_get(); \
260         o_##name pargs; \
261         pcounters[cnt] += pcnt_get() - pc_start; \
262 }
263
264 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
265 extern retn (*name) args; \
266 static retn (*o_##name) args; \
267 static retn w_##name args \
268 { \
269         retn ret; \
270         unsigned int pc_start = pcnt_get(); \
271         ret = o_##name pargs; \
272         pcounters[cnt] += pcnt_get() - pc_start; \
273         return ret; \
274 }
275
276 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
277 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
278 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
279 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
280 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
281 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
282 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
283 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
284
285 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
286 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
287 pc_hook_func              (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
288 pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
289 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
290 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
291 pc_hook_func              (SPU_playADPCMchannel, (void *a0), (a0), PCNT_SPU)
292 pc_hook_func              (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
293 pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1), (a0, a1), PCNT_SPU)
294
295 #define hook_it(name) { \
296         o_##name = name; \
297         name = w_##name; \
298 }
299
300 void pcnt_hook_plugins(void)
301 {
302         pcnt_init();
303
304         hook_it(GPU_writeStatus);
305         hook_it(GPU_writeData);
306         hook_it(GPU_writeDataMem);
307         hook_it(GPU_readStatus);
308         hook_it(GPU_readData);
309         hook_it(GPU_readDataMem);
310         hook_it(GPU_dmaChain);
311         hook_it(GPU_updateLace);
312         hook_it(SPU_writeRegister);
313         hook_it(SPU_readRegister);
314         hook_it(SPU_writeDMA);
315         hook_it(SPU_readDMA);
316         hook_it(SPU_writeDMAMem);
317         hook_it(SPU_readDMAMem);
318         hook_it(SPU_playADPCMchannel);
319         hook_it(SPU_async);
320         hook_it(SPU_playCDDAchannel);
321 }
322
323 // hooked into recompiler
324 void pcnt_gte_start(int op)
325 {
326         pcnt_start(PCNT_GTE);
327 }
328
329 void pcnt_gte_end(int op)
330 {
331         pcnt_end(PCNT_GTE);
332 }
333
334 #endif