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