drc: fix p32 32K memset stupidity, don't use p32 instead
[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 "../libpcsxcore/psemu_plugin_defs.h"
15 #include "../libpcsxcore/system.h"
16 #include "../plugins/cdrcimg/cdrcimg.h"
17
18 static int dummy_func() {
19         return 0;
20 }
21
22 /* SPU */
23 extern long SPUopen(void);
24 extern long SPUinit(void);
25 extern long SPUshutdown(void);
26 extern long SPUclose(void);
27 extern void SPUplaySample(unsigned char);
28 extern void SPUwriteRegister(unsigned long, unsigned short);
29 extern unsigned short SPUreadRegister(unsigned long);
30 extern void SPUwriteDMA(unsigned short);
31 extern unsigned short SPUreadDMA(void);
32 extern void SPUwriteDMAMem(unsigned short *, int);
33 extern void SPUreadDMAMem(unsigned short *, int);
34 extern void SPUplayADPCMchannel(void *);
35 extern void SPUregisterCallback(void (*callback)(void));
36 extern long SPUconfigure(void);
37 extern long SPUtest(void);
38 extern void SPUabout(void);
39 extern long SPUfreeze(unsigned int, void *);
40 extern void SPUasync(unsigned int);
41 extern void SPUplayCDDAchannel(short *, int);
42
43 /* PAD */
44 static long PADreadPort1(PadDataS *pad)
45 {
46         pad->controllerType = in_type;
47         pad->buttonStatus = ~in_keystate;
48         if (in_type == PSE_PAD_TYPE_ANALOGPAD) {
49                 pad->leftJoyX = in_a1[0];
50                 pad->leftJoyY = in_a1[1];
51                 pad->rightJoyX = in_a2[0];
52                 pad->rightJoyY = in_a2[1];
53         }
54         return 0;
55 }
56
57 static long PADreadPort2(PadDataS *pad)
58 {
59         pad->controllerType = PSE_PAD_TYPE_STANDARD;
60         pad->buttonStatus = ~in_keystate >> 16;
61         return 0;
62 }
63
64 /* GPU */
65 extern long GPUopen(unsigned long *, char *, char *);
66 extern long GPUinit(void);
67 extern long GPUshutdown(void);
68 extern long GPUclose(void);
69 extern void GPUwriteStatus(uint32_t);
70 extern void GPUwriteData(uint32_t);
71 extern void GPUwriteDataMem(uint32_t *, int);
72 extern uint32_t GPUreadStatus(void);
73 extern uint32_t GPUreadData(void);
74 extern void GPUreadDataMem(uint32_t *, int);
75 extern long GPUdmaChain(uint32_t *,uint32_t);
76 extern void GPUupdateLace(void);
77 extern long GPUfreeze(uint32_t, void *);
78
79
80 #define DUMMY(id, name) \
81         { id, #name, dummy_func }
82
83 #define DIRECT(id, name) \
84         { id, #name, name }
85
86 #define DUMMY_GPU(name)  DUMMY(PLUGIN_GPU, name)
87 #define DUMMY_CDR(name)  DUMMY(PLUGIN_CDR, name)
88 #define DUMMY_PAD(name)  DUMMY(PLUGIN_PAD, name)
89 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
90 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
91 #define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
92
93 static const struct {
94         int id;
95         const char *name;
96         void *func;
97 } plugin_funcs[] = {
98         /* CDR */
99         DUMMY_CDR(CDRinit),
100         DUMMY_CDR(CDRshutdown),
101         DUMMY_CDR(CDRopen),
102         DUMMY_CDR(CDRclose),
103         DUMMY_CDR(CDRtest),
104         DUMMY_CDR(CDRgetTN),
105         DUMMY_CDR(CDRgetTD),
106         DUMMY_CDR(CDRreadTrack),
107         DUMMY_CDR(CDRgetBuffer),
108         DUMMY_CDR(CDRgetBufferSub),
109         DUMMY_CDR(CDRplay),
110         DUMMY_CDR(CDRstop),
111         DUMMY_CDR(CDRgetStatus),
112         DUMMY_CDR(CDRgetDriveLetter),
113         DUMMY_CDR(CDRconfigure),
114         DUMMY_CDR(CDRabout),
115         DUMMY_CDR(CDRsetfilename),
116         DUMMY_CDR(CDRreadCDDA),
117         DUMMY_CDR(CDRgetTE),
118         /* SPU */
119         DIRECT_SPU(SPUconfigure),
120         DIRECT_SPU(SPUabout),
121         DIRECT_SPU(SPUinit),
122         DIRECT_SPU(SPUshutdown),
123         DIRECT_SPU(SPUtest),
124         DIRECT_SPU(SPUopen),
125         DIRECT_SPU(SPUclose),
126 //      DIRECT_SPU(SPUplaySample), // unused?
127         DIRECT_SPU(SPUwriteRegister),
128         DIRECT_SPU(SPUreadRegister),
129         DIRECT_SPU(SPUwriteDMA),
130         DIRECT_SPU(SPUreadDMA),
131         DIRECT_SPU(SPUwriteDMAMem),
132         DIRECT_SPU(SPUreadDMAMem),
133         DIRECT_SPU(SPUplayADPCMchannel),
134         DIRECT_SPU(SPUfreeze),
135         DIRECT_SPU(SPUregisterCallback),
136         DIRECT_SPU(SPUasync),
137         DIRECT_SPU(SPUplayCDDAchannel),
138         /* PAD */
139         DUMMY_PAD(PADinit),
140         DUMMY_PAD(PADshutdown),
141         DUMMY_PAD(PADopen),
142         DUMMY_PAD(PADclose),
143         DUMMY_PAD(PADsetSensitive),
144         DIRECT_PAD(PADreadPort1),
145         DIRECT_PAD(PADreadPort2),
146 /*
147         DUMMY_PAD(PADquery),
148         DUMMY_PAD(PADconfigure),
149         DUMMY_PAD(PADtest),
150         DUMMY_PAD(PADabout),
151         DUMMY_PAD(PADkeypressed),
152         DUMMY_PAD(PADstartPoll),
153         DUMMY_PAD(PADpoll),
154 */
155         /* GPU */
156         DIRECT_GPU(GPUupdateLace),
157         DIRECT_GPU(GPUinit),
158         DIRECT_GPU(GPUshutdown),
159         DIRECT_GPU(GPUopen),
160         DIRECT_GPU(GPUclose),
161         DIRECT_GPU(GPUreadStatus),
162         DIRECT_GPU(GPUreadData),
163         DIRECT_GPU(GPUreadDataMem),
164         DIRECT_GPU(GPUwriteStatus),
165         DIRECT_GPU(GPUwriteData),
166         DIRECT_GPU(GPUwriteDataMem),
167         DIRECT_GPU(GPUdmaChain),
168         DIRECT_GPU(GPUfreeze),
169
170         DUMMY_GPU(GPUdisplayText),
171 /*
172         DIRECT_GPU(GPUkeypressed),
173         DIRECT_GPU(GPUmakeSnapshot),
174         DIRECT_GPU(GPUconfigure),
175         DIRECT_GPU(GPUtest),
176         DIRECT_GPU(GPUabout),
177         DIRECT_GPU(GPUgetScreenPic),
178         DIRECT_GPU(GPUshowScreenPic),
179 */
180 //      DIRECT_GPU(GPUclearDynarec),
181 //      DIRECT_GPU(GPUvBlank),
182 };
183
184 void *plugin_link(enum builtint_plugins_e id, const char *sym)
185 {
186         int i;
187
188         if (id == PLUGIN_CDRCIMG)
189                 return cdrcimg_get_sym(sym);
190
191         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
192                 if (id != plugin_funcs[i].id)
193                         continue;
194
195                 if (strcmp(sym, plugin_funcs[i].name) != 0)
196                         continue;
197
198                 return plugin_funcs[i].func;
199         }
200
201         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
202         return NULL;
203 }
204
205 void plugin_call_rearmed_cbs(void)
206 {
207         extern void *hGPUDriver;
208         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
209
210         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
211         if (rearmed_set_cbs != NULL)
212                 rearmed_set_cbs(&pl_rearmed_cbs);
213 }
214
215 #ifdef PCNT
216
217 /* basic profile stuff */
218 #include "pcnt.h"
219
220 unsigned int pcounters[PCNT_CNT];
221 unsigned int pcounter_starts[PCNT_CNT];
222
223 #define pc_hook_func(name, args, pargs, cnt) \
224 extern void (*name) args; \
225 static void (*o_##name) args; \
226 static void w_##name args \
227 { \
228         unsigned int pc_start = pcnt_get(); \
229         o_##name pargs; \
230         pcounters[cnt] += pcnt_get() - pc_start; \
231 }
232
233 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
234 extern retn (*name) args; \
235 static retn (*o_##name) args; \
236 static retn w_##name args \
237 { \
238         retn ret; \
239         unsigned int pc_start = pcnt_get(); \
240         ret = o_##name pargs; \
241         pcounters[cnt] += pcnt_get() - pc_start; \
242         return ret; \
243 }
244
245 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
246 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
247 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
248 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
249 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
250 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
251 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
252 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
253
254 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1), (a0, a1), PCNT_SPU)
255 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
256 pc_hook_func              (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
257 pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
258 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1), (a0, a1), PCNT_SPU)
259 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1), (a0, a1), PCNT_SPU)
260 pc_hook_func              (SPU_playADPCMchannel, (void *a0), (a0), PCNT_SPU)
261 pc_hook_func              (SPU_async, (unsigned int a0), (a0), PCNT_SPU)
262 pc_hook_func              (SPU_playCDDAchannel, (short *a0, int a1), (a0, a1), PCNT_SPU)
263
264 #define hook_it(name) { \
265         o_##name = name; \
266         name = w_##name; \
267 }
268
269 void pcnt_hook_plugins(void)
270 {
271         /* test it first */
272         pcnt_get();
273
274         hook_it(GPU_writeStatus);
275         hook_it(GPU_writeData);
276         hook_it(GPU_writeDataMem);
277         hook_it(GPU_readStatus);
278         hook_it(GPU_readData);
279         hook_it(GPU_readDataMem);
280         hook_it(GPU_dmaChain);
281         hook_it(GPU_updateLace);
282         hook_it(SPU_writeRegister);
283         hook_it(SPU_readRegister);
284         hook_it(SPU_writeDMA);
285         hook_it(SPU_readDMA);
286         hook_it(SPU_writeDMAMem);
287         hook_it(SPU_readDMAMem);
288         hook_it(SPU_playADPCMchannel);
289         hook_it(SPU_async);
290         hook_it(SPU_playCDDAchannel);
291 }
292
293 #endif