Added debian_maemo to build the maemo package
[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 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 int  SPUplayCDDAchannel(short *, int);
42
43 /* PAD */
44 static long PADreadPort1(PadDataS *pad)
45 {
46         pad->controllerType = in_type1;
47         pad->buttonStatus = ~in_keystate;
48         if (in_type1 == 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 = in_type2;
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 extern void GPUvBlank(int, int);
79 extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
80
81
82 #define DUMMY(id, name) \
83         { id, #name, dummy_func }
84
85 #define DIRECT(id, name) \
86         { id, #name, name }
87
88 #define DUMMY_GPU(name)  DUMMY(PLUGIN_GPU, name)
89 #define DUMMY_CDR(name)  DUMMY(PLUGIN_CDR, name)
90 #define DUMMY_PAD(name)  DUMMY(PLUGIN_PAD, name)
91 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
92 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
93 #define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
94
95 static const struct {
96         int id;
97         const char *name;
98         void *func;
99 } plugin_funcs[] = {
100         /* CDR */
101         DUMMY_CDR(CDRinit),
102         DUMMY_CDR(CDRshutdown),
103         DUMMY_CDR(CDRopen),
104         DUMMY_CDR(CDRclose),
105         DUMMY_CDR(CDRtest),
106         DUMMY_CDR(CDRgetTN),
107         DUMMY_CDR(CDRgetTD),
108         DUMMY_CDR(CDRreadTrack),
109         DUMMY_CDR(CDRgetBuffer),
110         DUMMY_CDR(CDRgetBufferSub),
111         DUMMY_CDR(CDRplay),
112         DUMMY_CDR(CDRstop),
113         DUMMY_CDR(CDRgetStatus),
114         DUMMY_CDR(CDRgetDriveLetter),
115         DUMMY_CDR(CDRconfigure),
116         DUMMY_CDR(CDRabout),
117         DUMMY_CDR(CDRsetfilename),
118         DUMMY_CDR(CDRreadCDDA),
119         DUMMY_CDR(CDRgetTE),
120         /* SPU */
121         DIRECT_SPU(SPUconfigure),
122         DIRECT_SPU(SPUabout),
123         DIRECT_SPU(SPUinit),
124         DIRECT_SPU(SPUshutdown),
125         DIRECT_SPU(SPUtest),
126         DIRECT_SPU(SPUopen),
127         DIRECT_SPU(SPUclose),
128 //      DIRECT_SPU(SPUplaySample), // unused?
129         DIRECT_SPU(SPUwriteRegister),
130         DIRECT_SPU(SPUreadRegister),
131         DIRECT_SPU(SPUwriteDMA),
132         DIRECT_SPU(SPUreadDMA),
133         DIRECT_SPU(SPUwriteDMAMem),
134         DIRECT_SPU(SPUreadDMAMem),
135         DIRECT_SPU(SPUplayADPCMchannel),
136         DIRECT_SPU(SPUfreeze),
137         DIRECT_SPU(SPUregisterCallback),
138         DIRECT_SPU(SPUasync),
139         DIRECT_SPU(SPUplayCDDAchannel),
140         /* PAD */
141         DUMMY_PAD(PADinit),
142         DUMMY_PAD(PADshutdown),
143         DUMMY_PAD(PADopen),
144         DUMMY_PAD(PADclose),
145         DUMMY_PAD(PADsetSensitive),
146         DIRECT_PAD(PADreadPort1),
147         DIRECT_PAD(PADreadPort2),
148 /*
149         DUMMY_PAD(PADquery),
150         DUMMY_PAD(PADconfigure),
151         DUMMY_PAD(PADtest),
152         DUMMY_PAD(PADabout),
153         DUMMY_PAD(PADkeypressed),
154         DUMMY_PAD(PADstartPoll),
155         DUMMY_PAD(PADpoll),
156 */
157         /* GPU */
158         DIRECT_GPU(GPUupdateLace),
159         DIRECT_GPU(GPUinit),
160         DIRECT_GPU(GPUshutdown),
161         DIRECT_GPU(GPUopen),
162         DIRECT_GPU(GPUclose),
163         DIRECT_GPU(GPUreadStatus),
164         DIRECT_GPU(GPUreadData),
165         DIRECT_GPU(GPUreadDataMem),
166         DIRECT_GPU(GPUwriteStatus),
167         DIRECT_GPU(GPUwriteData),
168         DIRECT_GPU(GPUwriteDataMem),
169         DIRECT_GPU(GPUdmaChain),
170         DIRECT_GPU(GPUfreeze),
171         DIRECT_GPU(GPUvBlank),
172         DIRECT_GPU(GPUrearmedCallbacks),
173
174         DUMMY_GPU(GPUdisplayText),
175 /*
176         DIRECT_GPU(GPUkeypressed),
177         DIRECT_GPU(GPUmakeSnapshot),
178         DIRECT_GPU(GPUconfigure),
179         DIRECT_GPU(GPUtest),
180         DIRECT_GPU(GPUabout),
181         DIRECT_GPU(GPUgetScreenPic),
182         DIRECT_GPU(GPUshowScreenPic),
183 */
184 //      DIRECT_GPU(GPUclearDynarec),
185 };
186
187 void *plugin_link(enum builtint_plugins_e id, const char *sym)
188 {
189         int i;
190
191         if (id == PLUGIN_CDRCIMG)
192                 return cdrcimg_get_sym(sym);
193
194         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
195                 if (id != plugin_funcs[i].id)
196                         continue;
197
198                 if (strcmp(sym, plugin_funcs[i].name) != 0)
199                         continue;
200
201                 return plugin_funcs[i].func;
202         }
203
204         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
205         return NULL;
206 }
207
208 void plugin_call_rearmed_cbs(void)
209 {
210         extern void *hGPUDriver;
211         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
212
213         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
214         if (rearmed_set_cbs != NULL)
215                 rearmed_set_cbs(&pl_rearmed_cbs);
216 }
217
218 #ifdef PCNT
219
220 /* basic profile stuff */
221 #include "pcnt.h"
222
223 unsigned int pcounters[PCNT_CNT];
224 unsigned int pcounter_starts[PCNT_CNT];
225
226 #define pc_hook_func(name, args, pargs, cnt) \
227 extern void (*name) args; \
228 static void (*o_##name) args; \
229 static void w_##name args \
230 { \
231         unsigned int pc_start = pcnt_get(); \
232         o_##name pargs; \
233         pcounters[cnt] += pcnt_get() - pc_start; \
234 }
235
236 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
237 extern retn (*name) args; \
238 static retn (*o_##name) args; \
239 static retn w_##name args \
240 { \
241         retn ret; \
242         unsigned int pc_start = pcnt_get(); \
243         ret = o_##name pargs; \
244         pcounters[cnt] += pcnt_get() - pc_start; \
245         return ret; \
246 }
247
248 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
249 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
250 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
251 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
252 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
253 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
254 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
255 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
256
257 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1), (a0, a1), PCNT_SPU)
258 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
259 pc_hook_func              (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
260 pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
261 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1), (a0, a1), PCNT_SPU)
262 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1), (a0, a1), PCNT_SPU)
263 pc_hook_func              (SPU_playADPCMchannel, (void *a0), (a0), PCNT_SPU)
264 pc_hook_func              (SPU_async, (unsigned int a0), (a0), PCNT_SPU)
265 pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1), (a0, a1), PCNT_SPU)
266
267 #define hook_it(name) { \
268         o_##name = name; \
269         name = w_##name; \
270 }
271
272 void pcnt_hook_plugins(void)
273 {
274         pcnt_init();
275
276         hook_it(GPU_writeStatus);
277         hook_it(GPU_writeData);
278         hook_it(GPU_writeDataMem);
279         hook_it(GPU_readStatus);
280         hook_it(GPU_readData);
281         hook_it(GPU_readDataMem);
282         hook_it(GPU_dmaChain);
283         hook_it(GPU_updateLace);
284         hook_it(SPU_writeRegister);
285         hook_it(SPU_readRegister);
286         hook_it(SPU_writeDMA);
287         hook_it(SPU_readDMA);
288         hook_it(SPU_writeDMAMem);
289         hook_it(SPU_readDMAMem);
290         hook_it(SPU_playADPCMchannel);
291         hook_it(SPU_async);
292         hook_it(SPU_playCDDAchannel);
293 }
294
295 // hooked into recompiler
296 void pcnt_gte_start(int op)
297 {
298         pcnt_start(PCNT_GTE);
299 }
300
301 void pcnt_gte_end(int op)
302 {
303         pcnt_end(PCNT_GTE);
304 }
305
306 #endif