gpu: rework dma vs busy timing
[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 *, int32_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(GPUgetScreenPic),
223         DIRECT_GPU(GPUshowScreenPic),
224 */
225 };
226
227 void *plugin_link(enum builtint_plugins_e id, const char *sym)
228 {
229         int i;
230
231         if (id == PLUGIN_CDRCIMG)
232                 return cdrcimg_get_sym(sym);
233
234         for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
235                 if (id != plugin_funcs[i].id)
236                         continue;
237
238                 if (strcmp(sym, plugin_funcs[i].name) != 0)
239                         continue;
240
241                 return plugin_funcs[i].func;
242         }
243
244         //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
245         return NULL;
246 }
247
248 void plugin_call_rearmed_cbs(void)
249 {
250         extern void *hGPUDriver;
251         void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
252
253         pl_rearmed_cbs.screen_centering_type_default =
254                 Config.hacks.gpu_centering ? C_INGAME : C_AUTO;
255
256         rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
257         if (rearmed_set_cbs != NULL)
258                 rearmed_set_cbs(&pl_rearmed_cbs);
259 }
260
261 #ifdef PCNT
262
263 /* basic profile stuff */
264 #include "pcnt.h"
265
266 unsigned int pcounters[PCNT_CNT];
267 unsigned int pcounter_starts[PCNT_CNT];
268
269 #define pc_hook_func(name, args, pargs, cnt) \
270 extern void (*name) args; \
271 static void (*o_##name) args; \
272 static void w_##name args \
273 { \
274         unsigned int pc_start = pcnt_get(); \
275         o_##name pargs; \
276         pcounters[cnt] += pcnt_get() - pc_start; \
277 }
278
279 #define pc_hook_func_ret(retn, name, args, pargs, cnt) \
280 extern retn (*name) args; \
281 static retn (*o_##name) args; \
282 static retn w_##name args \
283 { \
284         retn ret; \
285         unsigned int pc_start = pcnt_get(); \
286         ret = o_##name pargs; \
287         pcounters[cnt] += pcnt_get() - pc_start; \
288         return ret; \
289 }
290
291 pc_hook_func              (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
292 pc_hook_func              (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
293 pc_hook_func              (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
294 pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
295 pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
296 pc_hook_func              (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
297 pc_hook_func_ret(long,     GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
298 pc_hook_func              (GPU_updateLace, (void), (), PCNT_GPU)
299
300 pc_hook_func              (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
301 pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0, , unsigned int a1), (a0, a1), PCNT_SPU)
302 pc_hook_func              (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
303 pc_hook_func              (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
304 pc_hook_func              (SPU_playADPCMchannel, (void *a0, unsigned int a1, int a2), (a0, a1, a2), PCNT_SPU)
305 pc_hook_func              (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
306 pc_hook_func_ret(int,      SPU_playCDDAchannel, (short *a0, int a1, unsigned int a2, int a3), (a0, a1, a2, a3), PCNT_SPU)
307
308 #define hook_it(name) { \
309         o_##name = name; \
310         name = w_##name; \
311 }
312
313 void pcnt_hook_plugins(void)
314 {
315         pcnt_init();
316
317         hook_it(GPU_writeStatus);
318         hook_it(GPU_writeData);
319         hook_it(GPU_writeDataMem);
320         hook_it(GPU_readStatus);
321         hook_it(GPU_readData);
322         hook_it(GPU_readDataMem);
323         hook_it(GPU_dmaChain);
324         hook_it(GPU_updateLace);
325         hook_it(SPU_writeRegister);
326         hook_it(SPU_readRegister);
327         hook_it(SPU_writeDMAMem);
328         hook_it(SPU_readDMAMem);
329         hook_it(SPU_playADPCMchannel);
330         hook_it(SPU_async);
331         hook_it(SPU_playCDDAchannel);
332 }
333
334 // hooked into recompiler
335 void pcnt_gte_start(int op)
336 {
337         pcnt_start(PCNT_GTE);
338 }
339
340 void pcnt_gte_end(int op)
341 {
342         pcnt_end(PCNT_GTE);
343 }
344
345 #endif