3263ae318f96b1f56c5b8e20c3b92cdbede4b5c0
[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 /* SPU */
24 #include "../plugins/dfsound/spu.h"
25
26 /* PAD */
27 long PAD1_readPort(PadDataS *pad) {
28         int pad_index = pad->requestPadIndex;
29
30         pad->controllerType = in_type[pad_index];
31         pad->buttonStatus = ~in_keystate[pad_index];
32
33         pad->portMultitap = multitap1;
34
35         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)
36         {
37                 pad->leftJoyX = in_analog_left[pad_index][0];
38                 pad->leftJoyY = in_analog_left[pad_index][1];
39                 pad->rightJoyX = in_analog_right[pad_index][0];
40                 pad->rightJoyY = in_analog_right[pad_index][1];
41
42                 pad->absoluteX = in_analog_left[pad_index][0];
43                 pad->absoluteY = in_analog_left[pad_index][1];
44         }
45
46         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
47         {
48                 pad->moveX = in_mouse[pad_index][0];
49                 pad->moveY = in_mouse[pad_index][1];
50         }
51
52         return 0;
53 }
54
55 long PAD2_readPort(PadDataS *pad) {
56         int pad_index = pad->requestPadIndex;
57
58         pad->controllerType = in_type[pad_index];
59         pad->buttonStatus = ~in_keystate[pad_index];
60
61         pad->portMultitap = multitap2;
62
63         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)
64         {
65                 pad->leftJoyX = in_analog_left[pad_index][0];
66                 pad->leftJoyY = in_analog_left[pad_index][1];
67                 pad->rightJoyX = in_analog_right[pad_index][0];
68                 pad->rightJoyY = in_analog_right[pad_index][1];
69
70                 pad->absoluteX = in_analog_left[pad_index][0];
71                 pad->absoluteY = in_analog_left[pad_index][1];
72         }
73
74         if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
75         {
76                 pad->moveX = in_mouse[pad_index][0];
77                 pad->moveY = in_mouse[pad_index][1];
78         }
79
80         return 0;
81 }
82
83 /* GPU */
84 extern long GPUopen(unsigned long *, char *, char *);
85 extern long GPUinit(void);
86 extern long GPUshutdown(void);
87 extern long GPUclose(void);
88 extern void GPUwriteStatus(uint32_t);
89 extern void GPUwriteData(uint32_t);
90 extern void GPUwriteDataMem(uint32_t *, int);
91 extern uint32_t GPUreadStatus(void);
92 extern uint32_t GPUreadData(void);
93 extern void GPUreadDataMem(uint32_t *, int);
94 extern long GPUdmaChain(uint32_t *, uint32_t, uint32_t *, int32_t *);
95 extern void GPUupdateLace(void);
96 extern long GPUfreeze(uint32_t, void *);
97 extern void GPUvBlank(int, int);
98 extern void GPUgetScreenInfo(int *y, int *base_hres);
99 extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
100
101
102 #define DIRECT(id, name) \
103         { id, #name, name }
104
105 #define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
106 #define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
107
108 static const struct {
109         int id;
110         const char *name;
111         void *func;
112 } plugin_funcs[] = {
113         /* SPU */
114         DIRECT_SPU(SPUinit),
115         DIRECT_SPU(SPUshutdown),
116         DIRECT_SPU(SPUopen),
117         DIRECT_SPU(SPUclose),
118         DIRECT_SPU(SPUwriteRegister),
119         DIRECT_SPU(SPUreadRegister),
120         DIRECT_SPU(SPUwriteDMAMem),
121         DIRECT_SPU(SPUreadDMAMem),
122         DIRECT_SPU(SPUplayADPCMchannel),
123         DIRECT_SPU(SPUfreeze),
124         DIRECT_SPU(SPUregisterCallback),
125         DIRECT_SPU(SPUregisterScheduleCb),
126         DIRECT_SPU(SPUasync),
127         DIRECT_SPU(SPUplayCDDAchannel),
128         DIRECT_SPU(SPUsetCDvol),
129         /* GPU */
130         DIRECT_GPU(GPUupdateLace),
131         DIRECT_GPU(GPUinit),
132         DIRECT_GPU(GPUshutdown),
133         DIRECT_GPU(GPUopen),
134         DIRECT_GPU(GPUclose),
135         DIRECT_GPU(GPUreadStatus),
136         DIRECT_GPU(GPUreadData),
137         DIRECT_GPU(GPUreadDataMem),
138         DIRECT_GPU(GPUwriteStatus),
139         DIRECT_GPU(GPUwriteData),
140         DIRECT_GPU(GPUwriteDataMem),
141         DIRECT_GPU(GPUdmaChain),
142         DIRECT_GPU(GPUfreeze),
143         DIRECT_GPU(GPUvBlank),
144         DIRECT_GPU(GPUgetScreenInfo),
145         DIRECT_GPU(GPUrearmedCallbacks),
146 };
147
148 void *plugin_link(enum builtint_plugins_e id, const char *sym)
149 {
150         int i;
151
152         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
153                 if (id != plugin_funcs[i].id)
154                         continue;
155
156                 if (strcmp(sym, plugin_funcs[i].name) != 0)
157                         continue;
158
159                 return plugin_funcs[i].func;
160         }
161
162         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
163         return NULL;
164 }
165
166 void plugin_call_rearmed_cbs(void)
167 {
168         extern void *hGPUDriver;
169         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
170
171         pl_rearmed_cbs.screen_centering_type_default =
172                 Config.hacks.gpu_centering ? C_INGAME : C_AUTO;
173
174         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
175         if (rearmed_set_cbs != NULL)
176                 rearmed_set_cbs(&pl_rearmed_cbs);
177 }
178
179 #ifdef PCNT
180
181 /* basic profile stuff */
182 #include "pcnt.h"
183
184 unsigned int pcounters[PCNT_CNT];
185 unsigned int pcounter_starts[PCNT_CNT];
186
187 #define pc_hook_func(name, args, pargs, cnt) \
188 extern void (*name) args; \
189 static void (*o_##name) args; \
190 static void w_##name args \
191 { \
192         unsigned int pc_start = pcnt_get(); \
193         o_##name pargs; \
194         pcounters[cnt] += pcnt_get() - pc_start; \
195 }
196
197 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
198 extern retn (*name) args; \
199 static retn (*o_##name) args; \
200 static retn w_##name args \
201 { \
202         retn ret; \
203         unsigned int pc_start = pcnt_get(); \
204         ret = o_##name pargs; \
205         pcounters[cnt] += pcnt_get() - pc_start; \
206         return ret; \
207 }
208
209 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
210 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
211 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
212 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
213 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
214 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
215 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
216 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
217
218 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
219 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0, , unsigned int a1), (a0, a1), PCNT_SPU)
220 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
221 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
222 pc_hook_func              (SPU_playADPCMchannel, (void *a0, unsigned int a1, int a2), (a0, a1, a2), PCNT_SPU)
223 pc_hook_func              (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
224 pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1, unsigned int a2, int a3), (a0, a1, a2, a3), PCNT_SPU)
225
226 #define hook_it(name) { \
227         o_##name = name; \
228         name = w_##name; \
229 }
230
231 void pcnt_hook_plugins(void)
232 {
233         pcnt_init();
234
235         hook_it(GPU_writeStatus);
236         hook_it(GPU_writeData);
237         hook_it(GPU_writeDataMem);
238         hook_it(GPU_readStatus);
239         hook_it(GPU_readData);
240         hook_it(GPU_readDataMem);
241         hook_it(GPU_dmaChain);
242         hook_it(GPU_updateLace);
243         hook_it(SPU_writeRegister);
244         hook_it(SPU_readRegister);
245         hook_it(SPU_writeDMAMem);
246         hook_it(SPU_readDMAMem);
247         hook_it(SPU_playADPCMchannel);
248         hook_it(SPU_async);
249         hook_it(SPU_playCDDAchannel);
250 }
251
252 // hooked into recompiler
253 void pcnt_gte_start(int op)
254 {
255         pcnt_start(PCNT_GTE);
256 }
257
258 void pcnt_gte_end(int op)
259 {
260         pcnt_end(PCNT_GTE);
261 }
262
263 #endif