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