apply cdrom volume in spu
[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
45 /* GPU */
46 static void CALLBACK GPUdisplayText(char *_) { return; }
47
48 /* SPU */
49 #include "../plugins/dfsound/spu.h"
50
51 /* PAD */
52 static long CALLBACK PADinit(long _) { return 0; }
53 static long CALLBACK PADopen(unsigned long *_) { return 0; }
54 static long CALLBACK PADshutdown(void) { return 0; }
55 static long CALLBACK PADclose(void) { return 0; }
56 static void CALLBACK PADsetSensitive(int _) { return; }
57
58 static long CALLBACK PADreadPort1(PadDataS *pad) {
59         int pad_index = pad->requestPadIndex;
60
61         pad->controllerType = in_type[pad_index];
62         pad->buttonStatus = ~in_keystate[pad_index];
63
64         pad->portMultitap = multitap1;
65
66         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)
67         {
68                 pad->leftJoyX = in_analog_left[pad_index][0];
69                 pad->leftJoyY = in_analog_left[pad_index][1];
70                 pad->rightJoyX = in_analog_right[pad_index][0];
71                 pad->rightJoyY = in_analog_right[pad_index][1];
72
73                 pad->absoluteX = in_analog_left[pad_index][0];
74                 pad->absoluteY = in_analog_left[pad_index][1];
75         }
76
77         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
78         {
79                 pad->moveX = in_mouse[pad_index][0];
80                 pad->moveY = in_mouse[pad_index][1];
81         }
82
83         return 0;
84 }
85
86 static long CALLBACK PADreadPort2(PadDataS *pad) {
87         int pad_index = pad->requestPadIndex;
88
89         pad->controllerType = in_type[pad_index];
90         pad->buttonStatus = ~in_keystate[pad_index];
91
92         pad->portMultitap = multitap2;
93
94         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)
95         {
96                 pad->leftJoyX = in_analog_left[pad_index][0];
97                 pad->leftJoyY = in_analog_left[pad_index][1];
98                 pad->rightJoyX = in_analog_right[pad_index][0];
99                 pad->rightJoyY = in_analog_right[pad_index][1];
100
101                 pad->absoluteX = in_analog_left[pad_index][0];
102                 pad->absoluteY = in_analog_left[pad_index][1];
103         }
104
105         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
106         {
107                 pad->moveX = in_mouse[pad_index][0];
108                 pad->moveY = in_mouse[pad_index][1];
109         }
110
111         return 0;
112 }
113
114 /* GPU */
115 extern long GPUopen(unsigned long *, char *, char *);
116 extern long GPUinit(void);
117 extern long GPUshutdown(void);
118 extern long GPUclose(void);
119 extern void GPUwriteStatus(uint32_t);
120 extern void GPUwriteData(uint32_t);
121 extern void GPUwriteDataMem(uint32_t *, int);
122 extern uint32_t GPUreadStatus(void);
123 extern uint32_t GPUreadData(void);
124 extern void GPUreadDataMem(uint32_t *, int);
125 extern long GPUdmaChain(uint32_t *, uint32_t, uint32_t *);
126 extern void GPUupdateLace(void);
127 extern long GPUfreeze(uint32_t, void *);
128 extern void GPUvBlank(int, int);
129 extern void GPUgetScreenInfo(int *y, int *base_hres);
130 extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
131
132
133 #define DIRECT(id, name) \
134         { id, #name, name }
135
136 #define DIRECT_CDR(name) DIRECT(PLUGIN_CDR, name)
137 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
138 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
139 #define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
140
141 static const struct {
142         int id;
143         const char *name;
144         void *func;
145 } plugin_funcs[] = {
146         /* CDR */
147         DIRECT_CDR(CDRinit),
148         DIRECT_CDR(CDRshutdown),
149         DIRECT_CDR(CDRopen),
150         DIRECT_CDR(CDRclose),
151         DIRECT_CDR(CDRtest),
152         DIRECT_CDR(CDRgetTN),
153         DIRECT_CDR(CDRgetTD),
154         DIRECT_CDR(CDRreadTrack),
155         DIRECT_CDR(CDRgetBuffer),
156         DIRECT_CDR(CDRgetBufferSub),
157         DIRECT_CDR(CDRplay),
158         DIRECT_CDR(CDRstop),
159         DIRECT_CDR(CDRgetStatus),
160         DIRECT_CDR(CDRgetDriveLetter),
161         DIRECT_CDR(CDRconfigure),
162         DIRECT_CDR(CDRabout),
163         DIRECT_CDR(CDRsetfilename),
164         DIRECT_CDR(CDRreadCDDA),
165         DIRECT_CDR(CDRgetTE),
166         /* SPU */
167         DIRECT_SPU(SPUinit),
168         DIRECT_SPU(SPUshutdown),
169         DIRECT_SPU(SPUopen),
170         DIRECT_SPU(SPUclose),
171         DIRECT_SPU(SPUwriteRegister),
172         DIRECT_SPU(SPUreadRegister),
173         DIRECT_SPU(SPUwriteDMAMem),
174         DIRECT_SPU(SPUreadDMAMem),
175         DIRECT_SPU(SPUplayADPCMchannel),
176         DIRECT_SPU(SPUfreeze),
177         DIRECT_SPU(SPUregisterCallback),
178         DIRECT_SPU(SPUregisterScheduleCb),
179         DIRECT_SPU(SPUasync),
180         DIRECT_SPU(SPUplayCDDAchannel),
181         DIRECT_SPU(SPUsetCDvol),
182         /* PAD */
183         DIRECT_PAD(PADinit),
184         DIRECT_PAD(PADshutdown),
185         DIRECT_PAD(PADopen),
186         DIRECT_PAD(PADclose),
187         DIRECT_PAD(PADsetSensitive),
188         DIRECT_PAD(PADreadPort1),
189         DIRECT_PAD(PADreadPort2),
190 /*
191         DIRECT_PAD(PADquery),
192         DIRECT_PAD(PADconfigure),
193         DIRECT_PAD(PADtest),
194         DIRECT_PAD(PADabout),
195         DIRECT_PAD(PADkeypressed),
196         DIRECT_PAD(PADstartPoll),
197         DIRECT_PAD(PADpoll),
198 */
199         /* GPU */
200         DIRECT_GPU(GPUupdateLace),
201         DIRECT_GPU(GPUinit),
202         DIRECT_GPU(GPUshutdown),
203         DIRECT_GPU(GPUopen),
204         DIRECT_GPU(GPUclose),
205         DIRECT_GPU(GPUreadStatus),
206         DIRECT_GPU(GPUreadData),
207         DIRECT_GPU(GPUreadDataMem),
208         DIRECT_GPU(GPUwriteStatus),
209         DIRECT_GPU(GPUwriteData),
210         DIRECT_GPU(GPUwriteDataMem),
211         DIRECT_GPU(GPUdmaChain),
212         DIRECT_GPU(GPUfreeze),
213         DIRECT_GPU(GPUvBlank),
214         DIRECT_GPU(GPUgetScreenInfo),
215         DIRECT_GPU(GPUrearmedCallbacks),
216
217         DIRECT_GPU(GPUdisplayText),
218 /*
219         DIRECT_GPU(GPUkeypressed),
220         DIRECT_GPU(GPUmakeSnapshot),
221         DIRECT_GPU(GPUconfigure),
222         DIRECT_GPU(GPUtest),
223         DIRECT_GPU(GPUabout),
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