2c95a67aff2bcca22c7607925c7136c420e0a0f9
[pcsx_rearmed.git] / 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 "../libpcsxcore/psxcommon.h"
17 #include "../plugins/cdrcimg/cdrcimg.h"
18
19 // this can't be __stdcall like it was in PSEmu API as too many functions are mixed up
20 #undef CALLBACK
21 #define CALLBACK
22
23 /* CDR */
24 struct CdrStat;
25 static long CALLBACK CDRinit(void) { return 0; }
26 static long CALLBACK CDRshutdown(void) { return 0; }
27 static long CALLBACK CDRopen(void) { return 0; }
28 static long CALLBACK CDRclose(void) { return 0; }
29 static long CALLBACK CDRgetTN(unsigned char *_) { return 0; }
30 static long CALLBACK CDRgetTD(unsigned char _, unsigned char *__) { return 0; }
31 static boolean CALLBACK CDRreadTrack(unsigned char *_) { return FALSE; }
32 static unsigned char * CALLBACK CDRgetBuffer(void) { return NULL; }
33 static unsigned char * CALLBACK CDRgetBufferSub(int sector) { return NULL; }
34 static long CALLBACK CDRconfigure(void) { return 0; }
35 static long CALLBACK CDRtest(void) { return 0; }
36 static void CALLBACK CDRabout(void) { return; }
37 static long CALLBACK CDRplay(unsigned char *_) { return 0; }
38 static long CALLBACK CDRstop(void) { return 0; }
39 static long CALLBACK CDRsetfilename(char *_) { return 0; }
40 static long CALLBACK CDRgetStatus(struct CdrStat *_) { return 0; }
41 static char * CALLBACK CDRgetDriveLetter(void) { return NULL; }
42 static long CALLBACK CDRreadCDDA(unsigned char _, unsigned char __, unsigned char ___, unsigned char *____) { return 0; }
43 static long CALLBACK CDRgetTE(unsigned char _, unsigned char *__, unsigned char *___, unsigned char *____) { return 0; }
44
45 /* GPU */
46 static void CALLBACK GPUdisplayText(char *_) { return; }
47
48 /* SPU */
49 extern long CALLBACK SPUopen(void);
50 extern long CALLBACK SPUinit(void);
51 extern long CALLBACK SPUshutdown(void);
52 extern long CALLBACK SPUclose(void);
53 extern void CALLBACK SPUwriteRegister(unsigned long, unsigned short, unsigned int);
54 extern unsigned short CALLBACK SPUreadRegister(unsigned long);
55 extern void CALLBACK SPUwriteDMAMem(unsigned short *, int, unsigned int);
56 extern void CALLBACK SPUreadDMAMem(unsigned short *, int, unsigned int);
57 extern void CALLBACK SPUplayADPCMchannel(void *, unsigned int, int);
58 extern void CALLBACK SPUregisterCallback(void (*cb)(void));
59 extern void CALLBACK SPUregisterScheduleCb(void (*cb)(unsigned int));
60 extern long CALLBACK SPUfreeze(unsigned int, void *, unsigned int);
61 extern void CALLBACK SPUasync(unsigned int, unsigned int);
62 extern int  CALLBACK SPUplayCDDAchannel(short *, int, unsigned int, int);
63
64 /* PAD */
65 static long CALLBACK PADinit(long _) { return 0; }
66 static long CALLBACK PADopen(unsigned long *_) { return 0; }
67 static long CALLBACK PADshutdown(void) { return 0; }
68 static long CALLBACK PADclose(void) { return 0; }
69 static void CALLBACK PADsetSensitive(int _) { return; }
70
71 static long CALLBACK PADreadPort1(PadDataS *pad) {
72         int pad_index = pad->requestPadIndex;
73
74         pad->controllerType = in_type[pad_index];
75         pad->buttonStatus = ~in_keystate[pad_index];
76
77         if (multitap1 == 1)
78                 pad->portMultitap = 1;
79         else
80                 pad->portMultitap = 0;
81
82         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 || in_type[pad_index] == PSE_PAD_TYPE_GUNCON)
83         {
84                 pad->leftJoyX = in_analog_left[pad_index][0];
85                 pad->leftJoyY = in_analog_left[pad_index][1];
86                 pad->rightJoyX = in_analog_right[pad_index][0];
87                 pad->rightJoyY = in_analog_right[pad_index][1];
88
89                 pad->absoluteX = in_analog_left[pad_index][0];
90                 pad->absoluteY = in_analog_left[pad_index][1];
91         }
92
93         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
94         {
95                 pad->moveX = in_mouse[pad_index][0];
96                 pad->moveY = in_mouse[pad_index][1];
97         }
98
99         return 0;
100 }
101
102 static long CALLBACK PADreadPort2(PadDataS *pad) {
103         int pad_index = pad->requestPadIndex;
104
105         pad->controllerType = in_type[pad_index];
106         pad->buttonStatus = ~in_keystate[pad_index];
107
108         if (multitap2 == 1)
109                 pad->portMultitap = 2;
110         else
111                 pad->portMultitap = 0;
112
113         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 || in_type[pad_index] == PSE_PAD_TYPE_GUNCON)
114         {
115                 pad->leftJoyX = in_analog_left[pad_index][0];
116                 pad->leftJoyY = in_analog_left[pad_index][1];
117                 pad->rightJoyX = in_analog_right[pad_index][0];
118                 pad->rightJoyY = in_analog_right[pad_index][1];
119
120                 pad->absoluteX = in_analog_left[pad_index][0];
121                 pad->absoluteY = in_analog_left[pad_index][1];
122         }
123
124         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
125         {
126                 pad->moveX = in_mouse[pad_index][0];
127                 pad->moveY = in_mouse[pad_index][1];
128         }
129
130         return 0;
131 }
132
133 /* GPU */
134 extern long GPUopen(unsigned long *, char *, char *);
135 extern long GPUinit(void);
136 extern long GPUshutdown(void);
137 extern long GPUclose(void);
138 extern void GPUwriteStatus(uint32_t);
139 extern void GPUwriteData(uint32_t);
140 extern void GPUwriteDataMem(uint32_t *, int);
141 extern uint32_t GPUreadStatus(void);
142 extern uint32_t GPUreadData(void);
143 extern void GPUreadDataMem(uint32_t *, int);
144 extern long GPUdmaChain(uint32_t *,uint32_t);
145 extern void GPUupdateLace(void);
146 extern long GPUfreeze(uint32_t, void *);
147 extern void GPUvBlank(int, int);
148 extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
149
150
151 #define DIRECT(id, name) \
152         { id, #name, name }
153
154 #define DIRECT_CDR(name) DIRECT(PLUGIN_CDR, name)
155 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
156 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
157 #define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
158
159 static const struct {
160         int id;
161         const char *name;
162         void *func;
163 } plugin_funcs[] = {
164         /* CDR */
165         DIRECT_CDR(CDRinit),
166         DIRECT_CDR(CDRshutdown),
167         DIRECT_CDR(CDRopen),
168         DIRECT_CDR(CDRclose),
169         DIRECT_CDR(CDRtest),
170         DIRECT_CDR(CDRgetTN),
171         DIRECT_CDR(CDRgetTD),
172         DIRECT_CDR(CDRreadTrack),
173         DIRECT_CDR(CDRgetBuffer),
174         DIRECT_CDR(CDRgetBufferSub),
175         DIRECT_CDR(CDRplay),
176         DIRECT_CDR(CDRstop),
177         DIRECT_CDR(CDRgetStatus),
178         DIRECT_CDR(CDRgetDriveLetter),
179         DIRECT_CDR(CDRconfigure),
180         DIRECT_CDR(CDRabout),
181         DIRECT_CDR(CDRsetfilename),
182         DIRECT_CDR(CDRreadCDDA),
183         DIRECT_CDR(CDRgetTE),
184         /* SPU */
185         DIRECT_SPU(SPUinit),
186         DIRECT_SPU(SPUshutdown),
187         DIRECT_SPU(SPUopen),
188         DIRECT_SPU(SPUclose),
189         DIRECT_SPU(SPUwriteRegister),
190         DIRECT_SPU(SPUreadRegister),
191         DIRECT_SPU(SPUwriteDMAMem),
192         DIRECT_SPU(SPUreadDMAMem),
193         DIRECT_SPU(SPUplayADPCMchannel),
194         DIRECT_SPU(SPUfreeze),
195         DIRECT_SPU(SPUregisterCallback),
196         DIRECT_SPU(SPUregisterScheduleCb),
197         DIRECT_SPU(SPUasync),
198         DIRECT_SPU(SPUplayCDDAchannel),
199         /* PAD */
200         DIRECT_PAD(PADinit),
201         DIRECT_PAD(PADshutdown),
202         DIRECT_PAD(PADopen),
203         DIRECT_PAD(PADclose),
204         DIRECT_PAD(PADsetSensitive),
205         DIRECT_PAD(PADreadPort1),
206         DIRECT_PAD(PADreadPort2),
207 /*
208         DIRECT_PAD(PADquery),
209         DIRECT_PAD(PADconfigure),
210         DIRECT_PAD(PADtest),
211         DIRECT_PAD(PADabout),
212         DIRECT_PAD(PADkeypressed),
213         DIRECT_PAD(PADstartPoll),
214         DIRECT_PAD(PADpoll),
215 */
216         /* GPU */
217         DIRECT_GPU(GPUupdateLace),
218         DIRECT_GPU(GPUinit),
219         DIRECT_GPU(GPUshutdown),
220         DIRECT_GPU(GPUopen),
221         DIRECT_GPU(GPUclose),
222         DIRECT_GPU(GPUreadStatus),
223         DIRECT_GPU(GPUreadData),
224         DIRECT_GPU(GPUreadDataMem),
225         DIRECT_GPU(GPUwriteStatus),
226         DIRECT_GPU(GPUwriteData),
227         DIRECT_GPU(GPUwriteDataMem),
228         DIRECT_GPU(GPUdmaChain),
229         DIRECT_GPU(GPUfreeze),
230         DIRECT_GPU(GPUvBlank),
231         DIRECT_GPU(GPUrearmedCallbacks),
232
233         DIRECT_GPU(GPUdisplayText),
234 /*
235         DIRECT_GPU(GPUkeypressed),
236         DIRECT_GPU(GPUmakeSnapshot),
237         DIRECT_GPU(GPUconfigure),
238         DIRECT_GPU(GPUtest),
239         DIRECT_GPU(GPUabout),
240         DIRECT_GPU(GPUgetScreenPic),
241         DIRECT_GPU(GPUshowScreenPic),
242 */
243 };
244
245 void *plugin_link(enum builtint_plugins_e id, const char *sym)
246 {
247         int i;
248
249         if (id == PLUGIN_CDRCIMG)
250                 return cdrcimg_get_sym(sym);
251
252         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
253                 if (id != plugin_funcs[i].id)
254                         continue;
255
256                 if (strcmp(sym, plugin_funcs[i].name) != 0)
257                         continue;
258
259                 return plugin_funcs[i].func;
260         }
261
262         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
263         return NULL;
264 }
265
266 void plugin_call_rearmed_cbs(void)
267 {
268         extern void *hGPUDriver;
269         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
270
271         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
272         if (rearmed_set_cbs != NULL)
273                 rearmed_set_cbs(&pl_rearmed_cbs);
274 }
275
276 #ifdef PCNT
277
278 /* basic profile stuff */
279 #include "pcnt.h"
280
281 unsigned int pcounters[PCNT_CNT];
282 unsigned int pcounter_starts[PCNT_CNT];
283
284 #define pc_hook_func(name, args, pargs, cnt) \
285 extern void (*name) args; \
286 static void (*o_##name) args; \
287 static void w_##name args \
288 { \
289         unsigned int pc_start = pcnt_get(); \
290         o_##name pargs; \
291         pcounters[cnt] += pcnt_get() - pc_start; \
292 }
293
294 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
295 extern retn (*name) args; \
296 static retn (*o_##name) args; \
297 static retn w_##name args \
298 { \
299         retn ret; \
300         unsigned int pc_start = pcnt_get(); \
301         ret = o_##name pargs; \
302         pcounters[cnt] += pcnt_get() - pc_start; \
303         return ret; \
304 }
305
306 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
307 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
308 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
309 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
310 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
311 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
312 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
313 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
314
315 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
316 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
317 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
318 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
319 pc_hook_func              (SPU_playADPCMchannel, (void *a0, unsigned int a1, int a2), (a0, a1, a2), PCNT_SPU)
320 pc_hook_func              (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
321 pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1, unsigned int a2, int a3), (a0, a1, a2, a3), PCNT_SPU)
322
323 #define hook_it(name) { \
324         o_##name = name; \
325         name = w_##name; \
326 }
327
328 void pcnt_hook_plugins(void)
329 {
330         pcnt_init();
331
332         hook_it(GPU_writeStatus);
333         hook_it(GPU_writeData);
334         hook_it(GPU_writeDataMem);
335         hook_it(GPU_readStatus);
336         hook_it(GPU_readData);
337         hook_it(GPU_readDataMem);
338         hook_it(GPU_dmaChain);
339         hook_it(GPU_updateLace);
340         hook_it(SPU_writeRegister);
341         hook_it(SPU_readRegister);
342         hook_it(SPU_writeDMAMem);
343         hook_it(SPU_readDMAMem);
344         hook_it(SPU_playADPCMchannel);
345         hook_it(SPU_async);
346         hook_it(SPU_playCDDAchannel);
347 }
348
349 // hooked into recompiler
350 void pcnt_gte_start(int op)
351 {
352         pcnt_start(PCNT_GTE);
353 }
354
355 void pcnt_gte_end(int op)
356 {
357         pcnt_end(PCNT_GTE);
358 }
359
360 #endif